创建索引:

db.集合.ensureIndex({属性:1}) #1表示升序,-1表示降序

具体操作:

  db.test.ensureIndex({name:1})

MongoDB在默认情况下索引字段的值可以相同

创建唯一索引(索引的值是唯一的)

db.test.ensureIndex({name:1},{"unique":true})

创建联合索引:

db.test.ensureIndex({name:1,age:1})

查看当前集合的所有索引:

db.test.getindexes()

删除索引:

db.test.dropIndex({"索引名称":1})

例如比较索引建立前后下面一条指令的查询时间:

db.test.find({name:"test10000"}).explain("executionStats") #.explain("executionStats")是用于显示查询过程的细节指令

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2019-02-12
  • 2022-12-23
  • 2021-04-23
  • 2021-09-21
猜你喜欢
  • 2021-11-05
  • 2021-07-18
  • 2021-09-29
  • 2021-05-27
  • 2022-12-23
  • 2021-08-10
  • 2021-08-28
相关资源
相似解决方案