mongodb索引(5)

1创建索引

    1是正序索引,-1是负序索引

    索引会提高查询时间按,降低插入时间

 db.books.ensureIndex({number:1})

2.创建索引,指定名称

db.book.ensureIndex({name:-1},{name:"bookname"})

3.唯一索引

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

4.剔除重复值

db.books.ensureIndex({name:1},{unique:true,dropDups:true})

5.指定索引进行查询

db.books.find({no:1}).hint({no:1})

6.查询目前是用了什么查询索引

 db.books.find({no:1}).explain()

7.产看索引

db.system.indexes.find()

8.创建索引会把表锁住,可以让创建索引放在后台进行解决

 db.books.ensureIndex({number:1},{background:true})

9.删除索引

 db.runCommand({dropIndexes:"books",index:"no_1"})

原文地址:https://www.cnblogs.com/youlangta/p/7345916.html