创建单列索引
1(升序);-1(降序)
db.t3.createIndex( { "id" : 1 } )
后台创建索引
db.t3.createIndex( { domainname: 1}, {background: true} )
创建子文档索引
db.people.createIndex( { "address.zipcode": 1 } )
查询子文档
db.users.find({"address.city":"Los Angeles"})
创建组合索引
db.t3.ensureIndex( { "id" : 1, "domainname" : 1 } );
创建唯一索引
db.t3.createIndex( { "id": 1 }, { unique: true } )
查看是否用到索引
db.t3.explain().help()
db.t3.find({id:1}).explain()
db.t3.find({id:{"$gt":1}}).explain()
db.t3.explain("executionStats").find({ quantity: { $gt: 1 }})
db.t3.explain("executionStats").find({"id": { $gt: 1 }})
db.t3.explain("allPlansExecution").find({"id": { $gt: 1 }})
Stages:
COLLSCAN for a full collection scan (did not use your index. You do NOT want this)
IXSCAN for scanning index keys (used the index to find)
FETCH for retrieving documents that it found (most of the time you will see this combined with IXSCAN)
查询已有索引
db.t3.getIndexes()
查询索引大小
db.t3.totalIndexSize()
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/91975/viewspace-2079583/
,如需转载,请注明出处,否则将追究法律责任。