《mongoDB》索引

一:基础操作

  • 创建单列索引
    • 语法:
         >db.collection.createIndex(keys, options)
         语法中 Key 值为你要创建的索引字段,1 为指定按升序创建索引,如果你想按降序来创建索引指定为 -1 即可。
      
      > db.demo.createIndex({"c" : 1})
      {
          "createdCollectionAutomatically" : false,
          "numIndexesBefore" : 5,
          "numIndexesAfter" : 6,
          "ok" : 1
      }
  • 同样,也可以创建复合索引
    • db.demo.createIndex({"title":1,"description":-1})
  • 查看集合索引
    • > db.demo.getIndexes()
  • 查看集合索引大小
    • > db.demo.totalIndexSize()
      118784
  • 删除集合所有索引
    • > db.demo.dropIndexes()
      {
      "nIndexesWas" : 5,
      "msg" : "non-_id indexes dropped for collection",
      "ok" : 1
      }
  • 删除集合指定索引
    • > db.demo.dropIndex("c_1")
      { "nIndexesWas" : 6, "ok" : 1 }

二:概念

  • 目前对于 mongo 的了解和应用欠缺,之后再使用和学习中在进行补充

 

posted @ 2019-02-13 13:33  Zzz哈  Views(125)  Comments(0Edit  收藏  举报