- 浏览: 83736 次
- 性别:
- 来自: 西安
最新评论
-
w67856101:
sssss
在Spring中使用jcaptcha实现图片验证 -
yjq8116:
不错。很有收获
Spring MVC 源码学习札记(四)HandlerMapping和HandlerAdapter(4) -
rolenz:
mongodb越来越火,但是网上的资料不是很全,希望深入的写些 ...
MongoDB学习(二)索引的用法 -
sha0k:
rolenz 写道能不能说说replSet到底如何进行认证我才 ...
MongoDB学习(一)安装和基本使用 -
rolenz:
能不能说说replSet到底如何进行认证
MongoDB学习(一)安装和基本使用
MongoDB与sql关于库表的名字:
database == database
collection == table
document == row
本篇转载自:http://blog.nosqlfan.com/html/271.html
索引能提高检索数据的速度,你可以想像成在MySQL中创建索引一样,同样索引也是用B-Tree也实现的。
1.单列索引
在字段x上创建索引,1 (表示升序) or -1 (表示降序)
> db.data.ensureIndex({x:1})
显示表data里面的所有索引
> db.data.getIndexes() [ { "name" : "_id_", "ns" : "recommender.data", "key" : { "_id" : 1 } }, { "_id" : ObjectId("4befb146b0e29ba1ce20e0bb"), "ns" : "recommender.data", "key" : { "x" : 1 }, "name" : "x_1" } ]
查找字段x为6的值,此时已经用到索引了
> db.data.find({x:6}) { "_id" : ObjectId("4bee804ba23d558eb6687117"), "x" : 6, "name" : "caihuafeng1" } { "_id" : ObjectId("4bee804ba23d558eb6687118"), "x" : 6, "name" : "caihuafeng2" } { "_id" : ObjectId("4bee804ba23d558eb6687119"), "x" : 6, "name" : "caihuafeng3" } { "_id" : ObjectId("4bee804ba23d558eb668711a"), "x" : 6, "name" : "caihuafeng4" } { "_id" : ObjectId("4bee804ba23d558eb668711b"), "x" : 6, "name" : "caihuafeng5" } { "_id" : ObjectId("4bee804ba23d558eb668711c"), "x" : 6, "name" : "caihuafeng6" } { "_id" : ObjectId("4bee804ba23d558eb668711d"), "x" : 6, "name" : "caihuafeng7" } { "_id" : ObjectId("4bee804ba23d558eb668711e"), "x" : 6, "name" : "caihuafeng8" } { "_id" : ObjectId("4bee804ba23d558eb668711f"), "x" : 6, "name" : "caihuafeng9" } { "_id" : ObjectId("4bee804ba23d558eb6687120"), "x" : 6, "name" : "caihuafeng10" }
2.默认索引
上述1中db.data.getIndexes()显示出来的一共有2个索引,其中_id是创建表的时候自动创建的索引,此索引是不能够删除的。
3.文档作为索引的键值
a.单列索引
往数据库recommender的表data中插入三条记录
> db.data.insert({name:"1616",info:{url:"http://www.1616.net/",city:"beijing"}}); > db.data.insert({name:"hao123",info:{url:"http://www.hao123.com/",city:"beijing"}}); > db.data.insert({name:"ll4la",info:{url:"http://www.114la.com/",city:"dongguan"}});
对字段info创建索引
> db.data.ensureIndex({info: 1});
显示表data上的所有索引
> db.data.getIndexes(); [ { "name" : "_id_", "ns" : "recommender.data", "key" : { "_id" : 1 } }, { "_id" : ObjectId("4befb146b0e29ba1ce20e0bb"), "ns" : "recommender.data", "key" : { "x" : 1 }, "name" : "x_1" }, { "_id" : ObjectId("4befb76bb0e29ba1ce20e0bf"), "ns" : "recommender.data", "key" : { "info" : 1 }, "name" : "info_1" } ]
查找指定的记录,此时会用到索引
> db.data.find({info: {url:"http://www.1616.net/",city:"beijing"}}); { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } }
b.组合索引
建立组合索引
> db.data.ensureIndex({"info.url":1, "info.city":1}); > db.data.getIndexes(); [ { "name" : "_id_", "ns" : "recommender.data", "key" : { "_id" : 1 } }, { "_id" : ObjectId("4befb146b0e29ba1ce20e0bb"), "ns" : "recommender.data", "key" : { "x" : 1 }, "name" : "x_1" }, { "_id" : ObjectId("4befb76bb0e29ba1ce20e0bf"), "ns" : "recommender.data", "key" : { "info" : 1 }, "name" : "info_1" }, { "_id" : ObjectId("4befb9d1b0e29ba1ce20e0c0"), "ns" : "recommender.data", "key" : { "info.url" : 1, "info.city" : 1 }, "name" : "info.url_1_info.city_1" } ]
下面几个操作均会用到索引
> db.data.find({"info.url": "http://www.1616.net/", "info.city": "beijing"}); { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } } > db.data.find({"info.url": "http://www.1616.net/"}); { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } }
1表示升序(asc),-1表示降序(desc)
> db.data.find({"info.url": /http:*/i}).sort({"info.url": 1, "info.city": 1}); { "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "name" : "ll4la", "info" : { "url" : "http://www.114la.com/", "city" : "dongguan" } } { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } } { "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "name" : "hao123", "info" : { "url" : "http://www.hao123.com/", "city" : "beijing" } } > db.data.find({"info.url": /http:*/i}).sort({"info.url": 1}); { "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "name" : "ll4la", "info" : { "url" : "http://www.114la.com/", "city" : "dongguan" } } { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } } { "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "name" : "hao123", "info" : { "url" : "http://www.hao123.com/", "city" : "beijing" } } > db.data.find({"info.url": /http:*/i}).sort({"info.url": -1}); { "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "name" : "hao123", "info" : { "url" : "http://www.hao123.com/", "city" : "beijing" } } { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } } { "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "name" : "ll4la", "info" : { "url" : "http://www.114la.com/", "city" : "dongguan" } }
4.组合索引
注意,这里的组合索引与上述3中的b中的组合索引是有点不同的,4里面是对一级字段建立组合索引,而上述3中是对二级字段建立组合索引。
在字段name及info上面创建组合索引
> db.data.ensureIndex({name: 1, info: -1});
MongoDB中索引的用法
本文是一篇转载文章,作者在对MongoDB 文档进行了细致的阅读后,总结出了MongoDB的各种索引的用法。
原文链接:http://iamcaihuafeng.blog.sohu.com/151638529.html
索引能提高检索数据的速度,你可以想像成在MySQL中创建索引一样,同样索引也是用B-Tree也实现的。
1.单列索引
在字段x上创建索引,1 (ascending) or -1 (descending)
> db.data.ensureIndex({x:1})
显示表data里面的所有索引
> db.data.getIndexes() [ { "name" : "_id_", "ns" : "recommender.data", "key" : { "_id" : 1 } }, { "_id" : ObjectId("4befb146b0e29ba1ce20e0bb"), "ns" : "recommender.data", "key" : { "x" : 1 }, "name" : "x_1" } ]
查找字段x为6的值,此时已经用到索引了
> db.data.find({x:6}) { "_id" : ObjectId("4bee804ba23d558eb6687117"), "x" : 6, "name" : "caihuafeng1" } { "_id" : ObjectId("4bee804ba23d558eb6687118"), "x" : 6, "name" : "caihuafeng2" } { "_id" : ObjectId("4bee804ba23d558eb6687119"), "x" : 6, "name" : "caihuafeng3" } { "_id" : ObjectId("4bee804ba23d558eb668711a"), "x" : 6, "name" : "caihuafeng4" } { "_id" : ObjectId("4bee804ba23d558eb668711b"), "x" : 6, "name" : "caihuafeng5" } { "_id" : ObjectId("4bee804ba23d558eb668711c"), "x" : 6, "name" : "caihuafeng6" } { "_id" : ObjectId("4bee804ba23d558eb668711d"), "x" : 6, "name" : "caihuafeng7" } { "_id" : ObjectId("4bee804ba23d558eb668711e"), "x" : 6, "name" : "caihuafeng8" } { "_id" : ObjectId("4bee804ba23d558eb668711f"), "x" : 6, "name" : "caihuafeng9" } { "_id" : ObjectId("4bee804ba23d558eb6687120"), "x" : 6, "name" : "caihuafeng10" }
2.默认索引
上述1中db.data.getIndexes()显示出来的一共有2个索引,其中_id是创建表的时候自动创建的索引,此索引是不能够删除的。
An index is always created on _id. This index is special and cannot be deleted. The _id index enforces uniqueness for its keys.
3.文档作为索引的键值
a.单列索引
MongoDB的官方文档上面是这样说的:
Documents as Keys
Indexed fields may be of any type, including documents:
往数据库recommender的表data中插入三条记录
> db.data.insert({name:"1616",info:{url:"http://www.1616.net/",city:"beijing"}}); > db.data.insert({name:"hao123",info:{url:"http://www.hao123.com/",city:"beijing"}}); > db.data.insert({name:"ll4la",info:{url:"http://www.114la.com/",city:"dongguan"}});
对字段info创建索引
> db.data.ensureIndex({info: 1});
显示表data上的所有索引
> db.data.getIndexes(); [ { "name" : "_id_", "ns" : "recommender.data", "key" : { "_id" : 1 } }, { "_id" : ObjectId("4befb146b0e29ba1ce20e0bb"), "ns" : "recommender.data", "key" : { "x" : 1 }, "name" : "x_1" }, { "_id" : ObjectId("4befb76bb0e29ba1ce20e0bf"), "ns" : "recommender.data", "key" : { "info" : 1 }, "name" : "info_1" } ]
查找指定的记录,此时会用到索引
> db.data.find({info: {url:"http://www.1616.net/",city:"beijing"}}); { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } }
b.组合索引
建立组合索引
> db.data.ensureIndex({"info.url":1, "info.city":1}); > db.data.getIndexes(); [ { "name" : "_id_", "ns" : "recommender.data", "key" : { "_id" : 1 } }, { "_id" : ObjectId("4befb146b0e29ba1ce20e0bb"), "ns" : "recommender.data", "key" : { "x" : 1 }, "name" : "x_1" }, { "_id" : ObjectId("4befb76bb0e29ba1ce20e0bf"), "ns" : "recommender.data", "key" : { "info" : 1 }, "name" : "info_1" }, { "_id" : ObjectId("4befb9d1b0e29ba1ce20e0c0"), "ns" : "recommender.data", "key" : { "info.url" : 1, "info.city" : 1 }, "name" : "info.url_1_info.city_1" } ]
下面几个操作均会用到索引
> db.data.find({"info.url": "http://www.1616.net/", "info.city": "beijing"}); { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } } > db.data.find({"info.url": "http://www.1616.net/"}); { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } }
1表示升序(asc),-1表示降序(desc)
> db.data.find({"info.url": /http:*/i}).sort({"info.url": 1, "info.city": 1}); { "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "name" : "ll4la", "info" : { "url" : "http://www.114la.com/", "city" : "dongguan" } } { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } } { "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "name" : "hao123", "info" : { "url" : "http://www.hao123.com/", "city" : "beijing" } } > db.data.find({"info.url": /http:*/i}).sort({"info.url": 1}); { "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "name" : "ll4la", "info" : { "url" : "http://www.114la.com/", "city" : "dongguan" } } { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } } { "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "name" : "hao123", "info" : { "url" : "http://www.hao123.com/", "city" : "beijing" } } > db.data.find({"info.url": /http:*/i}).sort({"info.url": -1}); { "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "name" : "hao123", "info" : { "url" : "http://www.hao123.com/", "city" : "beijing" } } { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } } { "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "name" : "ll4la", "info" : { "url" : "http://www.114 la.com/", "city" : "dongguan" } }
4.组合索引
注意,这里的组合索引与上述3中的b中的组合索引是有点不同的,4里面是对一级字段建立组合索引,而上述3中是对二级字段建立组合索引。
在字段name及info上面创建组合索引
> db.data.ensureIndex({name: 1, info: -1});
显示所有的索引
> db.data.getIndexes(); [ { "name" : "_id_", "ns" : "recommender.data", "key" : { "_id" : 1 } }, { "_id" : ObjectId("4befb146b0e29ba1ce20e0bb"), "ns" : "recommender.data", "key" : { "x" : 1 }, "name" : "x_1" }, { "_id" : ObjectId("4befb76bb0e29ba1ce20e0bf"), "ns" : "recommender.data", "key" : { "info" : 1 }, "name" : "info_1" }, { "_id" : ObjectId("4befb9d1b0e29ba1ce20e0c0"), "ns" : "recommender.data", "key" : { "info.url" : 1, "info.city" : 1 }, "name" : "info.url_1_info.city_1" }, { "_id" : ObjectId("4befbfcfb0e29ba1ce20e0c1"), "ns" : "recommender.data", "key" : { "name" : 1, "info" : -1 }, "name" : "name_1_info_-1" } ]
下面的排序将用到上面的索引
> db.data.find({"info.url": /http:*/i}).sort({name:1, info: -1}); { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } } { "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "name" : "hao123", "info" : { "url" : "http://www.hao123.com/", "city" : "beijing" } } { "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "name" : "ll4la", "info" : { "url" : "http://www.114la.com/", "city" : "dongguan" } }
5.唯一索引
往表data中插入一条记录。
> db.data.insert({firstname: "cai", lastname: "huafeng"});
由于表data中只有一记录有字段firstname及lastname,其它的行均没有相应的值,也就是均为null,为null就说明是相同的,而唯一索引是不允许有相同的值的,所以下面创建唯一组合索引时报错了。
所以建立唯一索引时,不管是对单个字段还是多个字段建立索引,则最好每一行均有此字段,否则会报错。
> db.data.find(); { "_id" : ObjectId("4bee745a0863b1c233b8b7ea"), "name" : "caihuafeng" } { "_id" : ObjectId("4bee745f0863b1c233b8b7eb"), "website" : "1616.net" } { "_id" : ObjectId("4bee804ba23d558eb6687117"), "x" : 6, "name" : "caihuafeng1" } { "_id" : ObjectId("4bee804ba23d558eb6687118"), "x" : 6, "name" : "caihuafeng2" } { "_id" : ObjectId("4bee804ba23d558eb6687119"), "x" : 6, "name" : "caihuafeng3" } { "_id" : ObjectId("4bee804ba23d558eb668711a"), "x" : 6, "name" : "caihuafeng4" } { "_id" : ObjectId("4bee804ba23d558eb668711b"), "x" : 6, "name" : "caihuafeng5" } { "_id" : ObjectId("4bee804ba23d558eb668711c"), "x" : 6, "name" : "caihuafeng6" } { "_id" : ObjectId("4bee804ba23d558eb668711d"), "x" : 6, "name" : "caihuafeng7" } { "_id" : ObjectId("4bee804ba23d558eb668711e"), "x" : 6, "name" : "caihuafeng8" } { "_id" : ObjectId("4bee804ba23d558eb668711f"), "x" : 6, "name" : "caihuafeng9" } { "_id" : ObjectId("4bee804ba23d558eb6687120"), "x" : 6, "name" : "caihuafeng10" } { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http://www.1616.net/", "city" : "beijing" } } { "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "name" : "hao123", "info" : { "url" : "http://www.hao123.com/", "city" : "beijing" } } { "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "name" : "ll4la", "info" : { "url" : "http://www.114la.com/", "city" : "dongguan" } } { "_id" : ObjectId("4befc51ab0e29ba1ce20e0c2"), "firstname" : "cai", "lastname" : "huafeng" } > db.data.ensureIndex({firstname: 1, lastname: 1}, {unique: true}); E11000 duplicate key error index: recommender.data.$firstname_1_lastname_1 dup key: { : null, : null }
6.删除索引
a.删除某个表中的所有索引
db.collection.dropIndexes();
b.删除某个表中的单一索引
db.collection.dropIndex({x: 1, y: -1}) > db.data.dropIndex({firstname: 1, lastname: 1}); { "nIndexesWas" : 6, "ok" : 1 }
相关推荐
MongoDB支持的索引种类很多,诸如单键索引,复合索引,多键索引,TTL索引,文本索引,空间地理索引等。同时索引的属性可以具有唯一性,即唯一索引。唯一索引用于确保索引字段不存储重复的值,即强制索引字段的唯一性...
通过具体实例,我们将了解MongoDB聚合框架的使用方法,包括各种聚合管道阶段(如$match, $project, $group等)的运用。同时,深入探讨MongoDB索引的类型(如单键索引、复合索引、多键索引等),并分析索引的创建、...
本文实例讲述了pymongo为mongodb数据库添加索引的方法。分享给大家供大家参考。具体实现方法如下: ... 您可能感兴趣的文章:MongoDB索引使用详解MongoDB的基础查询和索引操作方法总结pymongo给mongodb创建索
MongoDB中的索引其实类似于关系型数据库,都是为了提高查询和排序的效率的,并且实现原理也基本一致。由于集合中的键(字段)可以是普通数据类型,也可以是子文档。MongoDB可以在各种类型的键上创建索引。下面分别讲解...
Mongodb通常会被用来存储缓存数据或大尺寸、低价值的数据,对于这些类型的数据,数据量往往非常大,如果不定期清理,不但会影响性能,也会浪费大量的系统资源。 ...创建TTL索引方法: 和普通索引的
本文讲述了关于MongoDB索引管理包括索引的创建、查看索引、删除索引各方面的命令及使用方法
主要给大家介绍了关于MongoDB通配符索引的相关资料,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
上周在 ruby-china 上发了帖子《MongoDB 那些坑》,反映相当热烈,许多回复很有见地,其中一位童鞋深入的提到 MongoDB 建索引方法的问题,引发我更深入的了解了 MongoDB 建索引的方法和一些注意事项。 在 《MongoDB ...
本文给大家详细介绍了MongoDB数据库中的索引的知识,优缺点以及使用技巧等方面,非常细致,有需要的小伙伴可以参考下
MongoDB介绍及安装 通过samus驱动实现基本数据操作 在MVC模式下通过Jqgrid表格操作MongoDB数据 用MongoDB的文档结构描述数据关系 MongoDB文件存取操作 MongoDB索引用法和效率分析
二部分介绍使用MongoDB进行开发,包括索引的概念以及各种特殊索引和集合的用法等。三部分讲述复制,包括副本集的相关概念、创建方法,与应用程序的交互等。四部分讨论分片,包括分片的配置,片键的选择,集群的管理...
本文实例讲述了pymongo给mongodb创建索引的简单实现方法。分享给大家供大家参考。具体如下: 下面的代码给user的user_name字段创建唯一索引 import pymongo ... 您可能感兴趣的文章:MongoDB索引使用详解Mo
有4种方法可以使用 1、mongostat工具 2、profile集合介绍 3、日志 4、explain分析 mongostat mongostat是mongodb自带的状态检测工具,在命令行下使用。它会间隔固定时间获取mongodb的当前运行状态,并输出。如果发现...
MongoDB中用于空间索引数据的多线程空间连接算法 安装 克隆此存储库并安装依赖项。 git clone https://github.com/UrbanSystemsLab/spatial-join-mongodb.git cd spatial-join-mongodb npm install 图层名称 对于...
使用Spring Data MongoDB和MongoDB 3.0 17.1.1。配置选项 17.1.2。WriteConcern和WriteConcernChecking 17.1.3。认证 17.1.4。服务器端验证 17.1.5。其他事情要注意 附录 附录A:命名空间参考 元素 附录B:Poppers...
755.5 地理空间索引 765.5.1 复合地理空间索引 785.5.2 地球不是二维平面 78第6章 聚合 796.1 count 796.2 distinct 796.3 group 806.3.1 使用完成器 826.3.2 将函数做为键使用 846.4 MapReduce...