MongoDB索引(Shell)

for(var i=0;i<100000;i++){
var patid="pat"+i;
var patname="name"+i;
var sex="M";
var age=parseInt(100*Math.random(i));
db.patient.insert({"patid":patid,"patname":patname,"sex":sex,"age":age});
}

#普通索引
db.patient.ensureIndex({"age":1})
#唯一索引
db.patient.ensureIndex({"patid":1},{"unique":true})
#复合索引
db.patient.ensureIndex({"patname":1,"age":1})

#解释执行计划
db.patient.find({"patid":"pat1000"}).explain();
db.patient.find({"age":99}).explain();
#指定索引进行查询
db.patient.find({"age":99}).hint({"patname":1,"age":1}).explain();

#枚举索引
db.patient.getIndexes();

#删除索引
db.patient.dropIndex("age_1");

Leave a Reply

Your email address will not be published. Required fields are marked *

*