MongoDB学习(二)索引的用法
MongoDB与sql关于库表的名字:
database == database
collection == table
document == row
本篇转载自:http://blog.nosqlfan.com/html/271.html
?
索引能提高检索数据的速度,你可以想像成在MySQL中创建索引一样,同样索引也是用B-Tree也实现的。
1.单列索引
在字段x上创建索引,1 (表示升序) or -1 (表示降序)
?查找字段x为6的值,此时已经用到索引了
?2.默认索引
上述1中db.data.getIndexes()显示出来的一共有2个索引,其中_id是创建表的时候自动创建的索引,此索引是不能够删除的。3.文档作为索引的键值
a.单列索引往数据库recommender的表data中插入三条记录
?查找指定的记录,此时会用到索引
b.组合索引
建立组合索引?下面几个操作均会用到索引
?1表示升序(asc),-1表示降序(desc)
?4.组合索引
注意,这里的组合索引与上述3中的b中的组合索引是有点不同的,4里面是对一级字段建立组合索引,而上述3中是对二级字段建立组合索引。在字段name及info上面创建组合索引
查找字段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 KeysIndexed 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});?
显示所有的索引
?下面的排序将用到上面的索引
?5.唯一索引
往表data中插入一条记录。
?6.删除索引
a.删除某个表中的所有索引??
?
?
?
?
?
?
1 楼 rolenz 2012-01-31 mongodb越来越火,但是网上的资料不是很全,希望深入的写些命令参数的作用和使用例子。持续关注