MongoDB数据库的索引操作(转)

简介:

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  ,如需转载请自行联系原作者

相关文章
|
9月前
|
存储 关系型数据库 MySQL
MySQL数据库索引的数据结构?
MySQL中默认使用B+tree索引,它是一种多路平衡搜索树,具有树高较低、检索速度快的特点。所有数据存储在叶子节点,非叶子节点仅作索引,且叶子节点形成双向链表,便于区间查询。
241 4
|
9月前
|
NoSQL MongoDB 数据库
数据库数据恢复—MongoDB数据库数据恢复案例
MongoDB数据库数据恢复环境: 一台操作系统为Windows Server的虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 工作人员在MongoDB服务仍然开启的情况下将MongoDB数据库文件拷贝到其他分区,数据复制完成后将MongoDB数据库原先所在的分区进行了格式化操作。 结果发现拷贝过去的数据无法使用。管理员又将数据拷贝回原始分区,MongoDB服务仍然无法使用,报错“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
9月前
|
缓存 NoSQL Linux
在CentOS 7系统中彻底移除MongoDB数据库的步骤
以上步骤完成后,MongoDB应该会从您的CentOS 7系统中被彻底移除。在执行上述操作前,请确保已经备份好所有重要数据以防丢失。这些步骤操作需要一些基本的Linux系统管理知识,若您对某一步骤不是非常清楚,请先进行必要的学习或咨询专业人士。在执行系统级操作时,推荐在实施前创建系统快照或备份,以便在出现问题时能够恢复到原先的状态。
892 79
|
9月前
|
存储 NoSQL MongoDB
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
366 8
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
|
8月前
|
运维 NoSQL 容灾
告别运维噩梦:手把手教你将自建 MongoDB 平滑迁移至云数据库
程序员为何逃离自建MongoDB?扩容困难、运维复杂、高可用性差成痛点。阿里云MongoDB提供分钟级扩容、自动诊断与高可用保障,助力企业高效运维、降本增效,实现数据库“无感运维”。
|
7月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
489 158
|
7月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
7月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1224 152
|
7月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
909 156
|
7月前
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(中)
使用MYSQL Report分析数据库性能
509 156

推荐镜像

更多