博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MongoDB数据库的索引操作(转)
阅读量:7260 次
发布时间:2019-06-29

本文共 2939 字,大约阅读时间需要 9 分钟。

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" }

插入了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" }
>

这时我们看到多了刚才我们设置的那个字段,这样在查询的时候,如果查询条件有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"
                         }
                 }
         ]
}

删除索引后:

> 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" }

通过以上可以看出,查询的条件中有索引时,查询走BtreeCursor 的索引,而没有索引时走BasicCursor

通常需要索引的字段是:
1.唯一键 _id 是默认被设置为索引的
2.需要被查找的字段应该建立索引,比如在find()里面的字段
3.需要被排序的字段应该建立索引。比如在sort()里面的字段
以上就是MongoDB

的索引操作

本文转自 不得闲 博客园博客,原文链接: http://www.cnblogs.com/DxSoft/archive/2010/10/21/1857364.html  ,如需转载请自行联系原作者

你可能感兴趣的文章
Nginx
查看>>
DataProtection设置问题引起不同ASP.NET Core站点无法共享用户验证Cookie
查看>>
Windows 上编译 corefx 源码生成 Linux 上可用的 System.Data.SqlClient.dll
查看>>
Sublime python 環境配置和交互加載
查看>>
Android Touch事件传递机制 一: OnTouch,OnItemClick(监听器),dispatchTouchEvent(伪生命周期)...
查看>>
十进制到62进制的转换
查看>>
python 后台运行命令
查看>>
【IOS】读取、保存图片的各种方法
查看>>
CCNA第二章
查看>>
CCNP路 由 选 择 原 理
查看>>
input 特殊字符限制
查看>>
String类的subString(i)方法(基于jdk 1.9)
查看>>
Java并发包--ConcurrentLinkedQueue
查看>>
vue.js组件命名
查看>>
python------栈和队列的实现
查看>>
第八章 self sizing cell
查看>>
Linux中Nginx中添加自签证书TLS
查看>>
DFS ZOJ 1002/HDOJ 1045 Fire Net
查看>>
BZOJ4946 & 洛谷3826 & UOJ318:[NOI2017]蔬菜——题解
查看>>
10. ZooKeeper之搭建伪集群模式。
查看>>