MongoDB 的索引创建

1. 创建哈希索引 --- 哈希索引相较于普通索引查询速度会快些(哈希散列分布)

eg:  db.ensureIndex({"created": "hashed"})


2. 查看查询计划

eg:  db.find(query).explain();

"cursor" : "BasicCursor", ----说明没有索引发挥作用
"nscannedObjects" : 1000 ---理论上要扫描多少行
"cursor" : "BtreeCursor sn_1", 用到的btree索引


3. 获取索引

eg:  db.getIndexes()


4. 删除单个索引

eg: db.dropIndex({filed:1/-1})



5. 删除全部索引

eg:  db.dropIndexes()


6. 创建唯一索引

eg:  db.ensureIndex({filed.subfield:1/-1}, {unique:true})


7. 重建索引   (由于集合字段更改等等原因 会存在索引碎片,使用该命令可以合理利用空间。)

db.collection.reIndex()

 

posted @ 2020-08-07 15:55  路口有雾  阅读(399)  评论(0编辑  收藏  举报