• 头歌平台-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});

    "

  • 相关阅读:
    python对数据的操作
    使用JSAPl来做一个倒计时的效果
    线性表但是是Java中数组实用使用
    耗时4个月,阿里架构师打造Java面试突击文档,10位朋友已拿offer
    😊SpringBoot 整合 Elasticsearch (超详细).md
    Golang 开发实战day04 - Standard Library
    三维地图开发平台-支持离线地图开发
    【SQL server】数据库、数据表的创建
    【go语言入门笔记】12、指针
    行列向量的维数和个数的关系【三秩相等作为桥梁】
  • 原文地址:https://blog.csdn.net/m0_51919640/article/details/127922647