1:查询某个集合当前的索引
db.collection.getIndexes()
2:创建索引
db.collection.createIndex(keys,options)

例子,1表示升序索引,-1表示降序索引,单字段索引升序降序没有区别
db.collection.createIndex({userId:1})
复合索引的创建
db.collection.createIndex({userId:1,name:-1})

3:移除索引
db.collection.dropIndex(index)
db.collection.dropIndex({userId:1})
db.collection.dropIndex("userId_1") //根据名字删除索引

4:删除所有索引 //id的索引不会被删除
db.collection.dropIndexes()

5:查看执行计划

6:涵盖索引,查询的字段和索引的字段是同一个字段。

7:统计
db.collection.count({userId:"1003"})

8:分页
db.collection.find().limit(2) //查询前两条

9:
db.collection.find().limit(2).skip(2) //跳过前两条,查询两条

10:排序查询,1表示升序,-1表示降序
db.collection.find().sort({userId:1,})

db.collection.find({},{userId:1}).sort({userId:1,name:-1})

相关文章:

  • 2021-04-10
  • 2021-08-26
  • 2021-11-26
  • 2022-03-05
  • 2021-07-30
  • 2022-12-23
  • 2021-10-25
猜你喜欢
  • 2021-11-10
  • 2021-06-15
  • 2021-08-21
  • 2022-02-11
  • 2021-05-02
  • 2021-10-28
  • 2022-01-26
相关资源
相似解决方案