mongodb基础学习

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

原文地址:https://www.cnblogs.com/mkl34367803/p/13138669.html