头歌平台-MongoDB 之滴滴、摩拜都在用的索引
创始人
2024-01-29 23:21:48
0

第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});

"

相关内容

热门资讯

小本创业投资项目2011年 小... 刚开始投资的时候可以试试小本投资,投资金额的大小决定了很多创业项目风险性,投资潜力,今天先为大家分享...
2011年在河北省小本创业投资... 从目前的创业项目看来,女性创业的项目:服装,小吃餐饮,化妆品等,偏门项目。1、想开服装店,如果是初次...
99年版100元值多少钱 99... 杨静国际项目融资经理,既有项目法人融资“往年高交会的主题‘坚持新开展理念、推进高质量开展’,以及以后...
小本创业致富好项目 不妨试试这... 餐饮粥火锅加盟重庆串串中式快餐品牌小型餐饮加盟小吃加盟铁锅焖面加盟中式快餐店十大咖啡品牌品牌快餐老火...
【烤肉创业】烤肉创业 十几张桌... 烤番薯加盟四川烤肉加盟烤吧加盟韩国烤肉加盟店烤面筋加盟正宗韩国烤肉加盟石板烤肉加盟烤猪脚加盟上海纸上...
2018年*新农村小本创业项目... 2018年*新农村小本创业项目有哪些呢?很多的创业者都想知道农村有哪些小本创业项目,但是不知道农村创...
这6个农村小本创业好项目 小本... ②小磨香油加工用小磨加工的香油、香油皮,具有色泽好、香味佳的特点,倍受城乡居民的青睐。开设香油加小加...
推荐几个靠谱的挣小本创业什么项... 人不能以挣钱为目的,但是又有谁不是在为挣钱而做着不同的努力呢。上班族整天辛苦的加班,但是工资报酬并不...
宝妈可以做的靠谱的小本创业项目... 随着现在科技技术的进步,我们喜欢用照片记录生活,不管是一个人的臭美,还是美美的旅游,和朋友的欢畅聚会...
小本创业干些什么好 个人小本创... 小本创业适合做些什么?无本创业项目有哪些呢?创业就是无际的天空,平时想要创业的人士们,想创业但残酷的...