Mongodb数据库的索引操作很简单,只需要把作为条件的字段设置为索引即可
> use user
switched to db user
> show collections
system.indexes
u_info
u_setting
> db.system.indexes.find(); 这是默认的索引(默认为_id为索引)
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
>
> db.u_info.insert({uid:1,name:"Falcon.C",address:"Beijing"});
> db.u_info.insert({uid:2,name:"sexMan",address:"Wuhan"});
> db.u_info.find();
{ "_id" : ObjectId("4b9cf280c84d7f20576c4df2"), "uid" : 1, "name" : "Falcon.C", "address" : "Beijing" }
{ "_id" : ObjectId("4b9cf284c84d7f20576c4df3"), "uid" : 2, "name" : "sexMan", "address" : "Wuhan" }
switched to db user
> show collections
system.indexes
u_info
u_setting
> db.system.indexes.find(); 这是默认的索引(默认为_id为索引)
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
>
> db.u_info.insert({uid:1,name:"Falcon.C",address:"Beijing"});
> db.u_info.insert({uid:2,name:"sexMan",address:"Wuhan"});
> db.u_info.find();
{ "_id" : ObjectId("4b9cf280c84d7f20576c4df2"), "uid" : 1, "name" : "Falcon.C", "address" : "Beijing" }
{ "_id" : ObjectId("4b9cf284c84d7f20576c4df3"), "uid" : 2, "name" : "sexMan", "address" : "Wuhan" }
插入了2条记录,我们来把uid设置为索引字段:
> db.u_info.ensureIndex({uid:1});
> db.u_info.ensureIndex({name:1});
> db.system.indexes.find();
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "ns" : "user.u_info", "key" : { "uid" : 1 }, "name" : "uid_1" }
{ "ns" : "user.u_info", "key" : { "name" : 1 }, "name" : "name_1" }
>
> db.u_info.ensureIndex({name:1});
> db.system.indexes.find();
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "ns" : "user.u_info", "key" : { "uid" : 1 }, "name" : "uid_1" }
{ "ns" : "user.u_info", "key" : { "name" : 1 }, "name" : "name_1" }
>
这时我们看到多了刚才我们设置的那个字段,这样在查询的时候,如果查询条件有uid字段或name字段,则走索引来进行查询
有索引:
> db.u_info.find({name:"Falcon.C"});
{ "_id" : ObjectId("4b9cf280c84d7f20576c4df2"), "uid" : 1, "name" : "Falcon.C", "address" : "Beijing" }
> db.u_info.find({name:"Falcon.C"}).explain();
{
"cursor" : "BtreeCursor name_1",
"startKey" : {
"name" : "Falcon.C"
},
"endKey" : {
"name" : "Falcon.C"
},
"nscanned" : 1,
"n" : 1,
"millis" : 0,
"allPlans" : [
{
"cursor" : "BtreeCursor name_1",
"startKey" : {
"name" : "Falcon.C"
},
"endKey" : {
"name" : "Falcon.C"
}
}
]
}
{ "_id" : ObjectId("4b9cf280c84d7f20576c4df2"), "uid" : 1, "name" : "Falcon.C", "address" : "Beijing" }
> db.u_info.find({name:"Falcon.C"}).explain();
{
"cursor" : "BtreeCursor name_1",
"startKey" : {
"name" : "Falcon.C"
},
"endKey" : {
"name" : "Falcon.C"
},
"nscanned" : 1,
"n" : 1,
"millis" : 0,
"allPlans" : [
{
"cursor" : "BtreeCursor name_1",
"startKey" : {
"name" : "Falcon.C"
},
"endKey" : {
"name" : "Falcon.C"
}
}
]
}
删除索引后:
> db.system.indexes.find();
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "ns" : "user.u_info", "key" : { "uid" : 1 }, "name" : "uid_1" }
{ "ns" : "user.u_info", "key" : { "name" : 1 }, "name" : "name_1" }
> db.u_info.dropIndex("name_1")
{ "nIndexesWas" : 3, "ok" : 1 }
> db.u_info.find({name:"Falcon.C"}).explain();
{
"cursor" : "BasicCursor",
"startKey" : {
},
"endKey" : {
},
"nscanned" : 2,
"n" : 1,
"millis" : 0,
"allPlans" : [
{
"cursor" : "BasicCursor",
"startKey" : {
},
"endKey" : {
}
}
]
}
> db.system.indexes.find();
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "ns" : "user.u_info", "key" : { "uid" : 1 }, "name" : "uid_1" }
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "ns" : "user.u_info", "key" : { "uid" : 1 }, "name" : "uid_1" }
{ "ns" : "user.u_info", "key" : { "name" : 1 }, "name" : "name_1" }
> db.u_info.dropIndex("name_1")
{ "nIndexesWas" : 3, "ok" : 1 }
> db.u_info.find({name:"Falcon.C"}).explain();
{
"cursor" : "BasicCursor",
"startKey" : {
},
"endKey" : {
},
"nscanned" : 2,
"n" : 1,
"millis" : 0,
"allPlans" : [
{
"cursor" : "BasicCursor",
"startKey" : {
},
"endKey" : {
}
}
]
}
> db.system.indexes.find();
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "ns" : "user.u_info", "key" : { "uid" : 1 }, "name" : "uid_1" }
通过以上可以看出,查询的条件中有索引时,查询走BtreeCursor 的索引,而没有索引时走BasicCursor
通常需要索引的字段是:
1.唯一键 _id 是默认被设置为索引的
2.需要被查找的字段应该建立索引,比如在find()里面的字段
3.需要被排序的字段应该建立索引。比如在sort()里面的字段
以上就是MongoDB
的索引操作