头歌平台-MongoDB 之滴滴、摩拜都在用的索引

第1关:了解并创建一个简单索引

 

> use test

switched to db test

> db.student.insert([{_id:1,name:"王小明",age:15,score:90},{_id:2,name:"周晓晓",age:18,score:86},{_id:3,name:"王敏",age:20,score:96},{_id:4,name:"李晓亮",age:15,score:74},{_id:5,name:"张青青",age:21,score:88}])

BulkWriteResult({

        "writeErrors" : [ ],

        "writeConcernErrors" : [ ],

        "nInserted" : 5,

        "nUpserted" : 0,

        "nMatched" : 0,

        "nModified" : 0,

        "nRemoved" : 0,

        "upserted" : [ ]

})

> db.student.createIndex({score:-1})

{

        "createdCollectionAutomatically" : false,

        "numIndexesBefore" : 1,

        "numIndexesAfter" : 2,

        "ok" : 1

}

第2关:常见索引的创建

编程要求

根据提示,在右侧命令行进行操作,在 test2 数据库中创建集合 article,内容如下:

_idtitletagsfollwers
1提升程序员工作效率的6个工具利器Alfred,幕布543
2我是如何从零开始学习前端的HTML,Html5,CSS1570
320个非常有用的JAVA程序片段Java,编程1920

集合创建完成后,按要求创建以下索引:

  • 用字段 follwers 和 title 创建复合升序索引;

  • 用字段 tags 创建多 key 降序索引;

  • _id创建哈希索引;

  • 用字段 title 和 tags 创建文本索引。

    > use test2

    switched to db test2

    > db.article.insert([

    {_id:1,title:"提升程序员工作效率的6个工具利器",tags:["Alfred","幕布"],follwers:543},

    {_id:2,title:"我是如何从零开始学习前端的",tags:["HTML","Html5","CSS"],follwers:1570},

    {_id:3,title:"20个非常有用的JAVA程序片段",tags:["Java","编程"],follwers:1920}])

    BulkWriteResult({

            "writeErrors" : [ ],

            "writeConcernErrors" : [ ],

            "nInserted" : 3,

            "nUpserted" : 0,

            "nMatched" : 0,

            "nModified" : 0,

            "nRemoved" : 0,

            "upserted" : [ ]

    })

    > db.article.createIndex({follwers:1,title:1})

    {

            "createdCollectionAutomatically" : false,

            "numIndexesBefore" : 1,

            "numIndexesAfter" : 2,

            "ok" : 1

    }

    > db.article.createIndex({tags:-1})

    {

            "createdCollectionAutomatically" : false,

            "numIndexesBefore" : 2,

            "numIndexesAfter" : 3,

            "ok" : 1

    }

    > db.article.createIndex({_id:'hashed'})

    {

            "createdCollectionAutomatically" : false,

            "numIndexesBefore" : 3,

            "numIndexesAfter" : 4,

            "ok" : 1

    }

    > db.article.createIndex({title:'text',tags:'text'})

    {

            "createdCollectionAutomatically" : false,

            "numIndexesBefore" : 4,

            "numIndexesAfter" : 5,

            "ok" : 1

    }

第3关:有趣的地理位置索引

编程要求

如图4所示,有6个人 A、B、C、D、E、F 的位置,请将这些位置信息插入到数据库 test3 的集合 people 中,并建立地理位置索引 personloc。

  • 查询 A 周围100~3000米有哪些人;

  • 查询 B 周围100~5000米有哪些人;

  • 查询 C 周围3000~8000米有哪些人;

  • 查询 D 周围3000~8000米有哪些人。

  • 请逐条插入以下信息和位置 GeoJson 数据:

     
  • {_id:1,name:'A',personloc:{type:'Point',coordinates:[116.403981,39.914935]}}
  • {_id:2,name:'B',personloc:{type:'Point',coordinates:[116.433733,39.909511]}}
  • {_id:3,name:'C',personloc:{type:'Point',coordinates:[116.488781,39.949901]}}
  • {_id:4,name:'D',personloc:{type:'Point',coordinates:[116.342609,39.948021]}}
  • {_id:5,name:'E',personloc:{type:'Point',coordinates:[116.328236,39.901098]}}
  • {_id:6,name:'F',personloc:{type:'Point',coordinates:[116.385728,39.871645]}}

echo "

db.people.insert({_id:1,name:'A',personloc:{type:'Point',coordinates:[116.403981,39.914935]}});

db.people.insert({_id:2,name:'B',personloc:{type:'Point',coordinates:[116.433733,39.909511]}});

db.people.insert({_id:3,name:'C',personloc:{type:'Point',coordinates:[116.488781,39.949901]}});

db.people.insert({_id:4,name:'D',personloc:{type:'Point',coordinates:[116.342609,39.948021]}});

db.people.insert({_id:5,name:'E',personloc:{type:'Point',coordinates:[116.328236,39.901098]}});

db.people.insert({_id:6,name:'F',personloc:{type:'Point',coordinates:[116.385728,39.871645]}});

db.people.createIndex({personloc:'2dsphere'});

db.runCommand({geoNear:'people',near:{type:'Point',coordinates:[116.403981,39.914935]},spherical:true,minDistance:100,maxDistance:3000});

db.runCommand({geoNear:'people',near:{type:'Point',coordinates:[116.433733,39.909511]},spherical:true,minDistance:100,maxDistance:5000});

db.runCommand({geoNear:'people',near:{type:'Point',coordinates:[116.488781,39.949901]},spherical:true,minDistance:3000,maxDistance:8000});

db.runCommand({geoNear:'people',near:{type:'Point',coordinates:[116.342609,39.948021]},spherical:true,minDistance:3000,maxDistance:8000});

"

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值