如何在MongoDB数据库中使用索引

分类:编程技术 时间:2024-02-20 15:25 浏览:0 评论:0
0
本文向您展示如何在 MongoDB 数据库中使用索引。内容简洁易懂。绝对会让你眼前一亮。希望您能从本文的详细介绍中有所收获。

MongoDB中各种索引的用途是什么?

1.单列索引

在字段x上创建索引,1(升序)或-1(降序)

db.data.ensureIndex( {x:1})

​显示表数据中所有索引

​> db.data.getIndexes()

​[

​{

“name”:“_id_”,

“ns”:“recommender.data”,

“key”:{

”_id” : 1

}

},

 {

“_id” : ObjectId("4befb146b0e29ba1ce20e0bb"),< /p>

"ns" : "recommender.data",

"key" : {

"x" : 1

},< /p>

“name” : “x_1”

★}

★]

查找字段x的值为6,索引已被使用此时

> db.data.find({x:6})

{ "_id" : ObjectId( "4bee804ba23d558eb6687117"), "x" : 6, "name ” : “彩花枫1" }

【{ "_id" : ObjectId("4bee804ba23d558eb6687118"), "x" : 6, "name" : "caihuafeng2" }

 { "_id" : ObjectId(" 4bee804ba23d558eb6687119"), "x" : 6, "名称" : "彩花风3" }

{ "_id" : ObjectId("4bee804ba23d558eb668711a"), "x" : 6, "名称" : "彩花风4" }

【{ "_id" : ObjectId("4bee804ba23d558eb668711b"), "x" : 6, "姓名" : "菜花凤5" }


{ "_id" : ObjectId("4bee804ba23d558eb668711c "), "x" : 6, "姓名" : "菜花枫6" }

【{ "_id" : ObjectId("4bee804ba23d558eb668711e"), " x" : 6, "name" : "菜花峰8" }


{ "_id" : ObjectId("4bee804ba23d558eb668711f" ), "x" : 6,"姓名" : "菜花风9" }

/p>

二、默认索引<​​/p>

上面1中db.data.getIndexes()显示的索引一共有2个,其中_id是建表时自动创建的索引创建了。这个我索引无法删除。

<索引总是在_id上创建。该索引比较特殊,不能删除。 _id 索引强制其键的唯一性。

【3. 文档作为索引的键值

p>

a. _id 索引强制其键的唯一性。单列索引

MongoDB的官方文档是这样说的:

文档作为键

索引字段可以是任何类型,包括文档:

向数据库推荐器的表数据中插入三条记录

> db.data.insert({name:"1616",info:{url:"http://www.1616.net/" ,city:"北京"}});

<> db.data.insert({name:"hao123",info:{url:"http://www.hao123.com/",city :"北京"}});

<> db.data.insert({name:"ll4la",info:{url:"http://www.114la.com/",city:" dongguan"}});

创建字段infoReference的搜索

 db.data.ensureIndex({info: 1});

 显示全部表数据索引

<> db.data.getIndexes( );

<[

<{

<"name" : "_id_" ,

“ns”:“recommender.data”,

“key”:{

『_id』:1

『}

『},

『{

『_id』:ObjectId("4befb146b0e29ba1ce20e0bb") ,

“ns”:“推荐器.data”,

“key”:{

“x”:1

<} ,

"名称" : "x_1"

★},

{

★"_id" : ObjectId("4befb76bb0e29ba1ce20e0bf"),

“ns”:“recommender.data”,

“key”:{

“info”:1

<},

【"name" : "info_1"

}

]

查找指定记录,此时会用到索引

<> db.data.find({info: {url:"http://www.1616.net/",城市:"北京"}});

<< "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "姓名" : "1616", "信息" : { "url" : "http://www.1616.net/", "城市" : "北京" } }

b.组合索引

创建组合索引

:db.data.ensureIndex({"info.url":1, "info.city":1});

< p><> db.data.getIndexes();

<[

<{

<"name" : "_id_",

" ns" : "recommender.data",

"key" : {

"_id" : 1

}

},

{

"_id" : ObjectId("4befb146b0e29ba1ce20e0bb"),

:“ns”:“推荐器.data”,

:“key”:{

:“x”:1

< p> },

<"名称": "x_1"

<},

<{

<"_id": ObjectId("4befb76bb0e29ba1ce20e0bf "),

“ns”:“推荐器.data”,

“key”:{

“info”:1

},

『名称』:『info_1』

『}』,

』{

『_id』:ObjectId("4befb9d1b0e29ba1ce20e0c0" ),

“ns”:“推荐器.data”,

“key”:{

“info.url”:1,

<"info.city" : 1

<},



< p>​]

​以下操作会用到索引

​> db.data.find({"info.url": "http://www.1616.net/", "info.city ": "北京"});

 { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "name" : "1616", "info" : { "url" : "http ://www.1616.net/", "城市" : "北京" } }

db.data.find({"info.url": "http://www.1616. net/"} );

 { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "名称" : "1616", "信息" : { "url" : "http://www.1616 .net/", "城市" : "是ijing" } }

​1表示升序(asc),-1表示降序(desc)

​​​ db.data.find({"info.url": /http :*/i})。排序({“ info.url”:1,“ info.city”:1});

{“ _id”:objectID(“ 4BEFB740B0E29BA1CE20E0BE”),” : "ll4la", "info" : { "url" : "http://www.114la.com/", "城市" : "东莞" } }

 { "_id" : ObjectId( "4befb711b0e29ba1ce20e0bc"), "名称" : "1616", "信息" : { "url" : "http://www.1616.net/ ", "城市" : "北京" } }

 { "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "名称" : "hao123", "info" : { "url" : "http://www.hao123.com/", "城市" : "北京" } }

< > db.data.find({"info.url": /http:*/i}).sort({"info.url": 1});

< p><{ "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "名称" : "ll4la", "信息" : { "url" : "http://www. 114la.com/“,“ city”:“ dongguan”}}

{“ _id”:objectid(“ 4BEFB711B0E29BA1CE20E0BC”),“名称”:“ 1616” : "http://www.1616.net/", "城市" : "北京" } }

{ "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "name" : "hao123", "info" : { "url" : "http://www.hao123.com/", "city" : "北京" } }

<> db. data.find({"info.url ": /http:*/i}).sort({"info.url": -1});

 { "_id" : ObjectId("4befb723b0e29ba1ce20e0bd "), "姓名" : "hao123 ", "信息" : { "url" : "http://www.hao123.com/", "城市" : "北京" } }

 { "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "姓名" : "1616", "信息" : { "url" : "http://www .1616.net/", "城市" : "北京" } }

 { "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "名称" : "ll4la", "信息" : { "url " : "http://www.114la.com/", " city" : "dongguan" } }

【MongoDB中各种索引的用途是什么

【4.组合索引

注意,这里的组合索引是一个与上面 3 中 b 中的组合索引没什么不同。 4中是对一级字段建立组合索引,而上述3中是对二级字段建立组合索引。索引。

在字段 name 和 info 上创建组合索引

> db.data.ensureIndex({name: 1, info: -1});

创建组合索引时,字段后面的1表示升序,-1表示降序。使用1还是-1主要和排序的时候或者指定范围内查询的时候有关。详细内容请参见下面的英文原文。

当创建索引时,与键关联的数字指定了索引的方向,因此它应该始终为 1(升序)或 -1(降序)。方向对于单键索引或随机访问检索并不重要,但如果您对复合索引进行排序或范围查询,方向就很重要。

【显示所有索引

】db.data。 getIndexes();

<{

<"name" : "_id_",

<"ns" : "recommender.data ",

<"key": {

<"_id": 1

<}

<},

{

“_id”:ObjectId(“4befb146b0e29ba1ce20e0bb”),

“ns”:“recommender.data”,

“key”:{

"x" : 1

},

"名称" : "x_1"

},

{

“_id”:ObjectId(“4befb76bb0e29ba1ce20e0bf”),

“ns”:“recommender.data”,

“key”:{

< p>“信息”:1< /p>

<},

<名称>:“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 ",

『键』:{

『名称』:1,

『信息』:-1

』},< /p>

“name” : “name_1_info_-1”

<}

]

下面的排序将使用上面的索引

最后一行的“名字”:“ll4la”其实是“名字”:“114la”(也就是数字一写成了字母l),但是我输入的时候却写成了“名称”:“ll4la”。我犯了一个错误,但排序结果是正确的。

> db.data.find({"info.url": /http:*/i}).sort({name:1, info: -1});

 { "_id" : 对象tId("4befb711b0e29ba1ce20e0bc"), "名称" : "1616", "信息" : { "url" : "http://www.1616.net/", "城市" : "北京" } }

<{ "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "姓名" : "hao123", "信息" : { "url" : "http://www.hao123.com/", "城市" : "北京" } }

{ "_id" : ObjectId("4befb740b0e29ba1ce20e0be"), "name" : "ll4la", "info" : { "url" : "http://www.114la.com /", "city" : "dongguan" } }

​MongoDB复合索引规则

​如果有多个字段的复合索引,可以用它来查询起始子集字段。因此,如果您在< /p>

<上有索引,则可以使用它查询

A ,b,c

如果您使用过 MySQL,它看起来很熟悉。原理与MySQL相同。

5.唯一索引

去往表data中插入一条记录。

★> db.data.insert({ Firstname: "cai", Lastname: "huafeng"});

由于表data中只有一条记录,有字段firstname和lastname,其他行没有c或者对应的值,即全部为null。如果为null,则相同,而唯一索引不允许取相同的值,所以下面创建唯一组合索引时会出错。 .

因此,创建唯一索引时,无论是单个字段还是多个字段,最好每一行都有这个字段,否则会报错。

  > db.data.find();

 { "_id" : ObjectId("4bee745a0863b1c233b8b7ea"), "name" : "彩花凤" }

  { "_id" : ObjectId ("4bee745f0863b1c233b8b7eb"), "网站" : "1616.net" }

​{ "_id" : ObjectId("4bee804ba23d558eb6687117"), "x" : 6, "姓名" : "彩花峰1" } < /p>

​{ "_id" : ObjectId("4bee804ba23d558eb6687118"), "x" : 6, "名称" : "菜花峰2" }

{ "_id" : ObjectId("4bee804ba23d558eb6687119" ) , "x" : 6, "姓名" : "菜花凤3" }

  

{ "_id" : ObjectId("4bee804ba23d558eb668711a"), "x" : 6, "姓名" : "菜花凤4" }

p>

<{ "_id" : ObjectId("4bee804ba23d558eb668711b"), "x" : 6, "名称" : "caihuafeng5" }

【{ "_id" : ObjectId("4bee804ba23d558eb668711c"), "x" : 6, "姓名" : "caihuafeng6" }

{ "_id" : ObjectId( "4bee804ba23d558eb668711d"), "x" : 6, "名称" : "caihuafeng7" }

 { "_id" : ObjectId("4bee804ba23d558eb668711e"), "x" : 6, "名称" : "caihuafeng8 " }

【{ "_id" : ObjectId ("4bee804ba23d558eb668711f"), "x" : 6, "name" : "caihuafeng9" }


{ "_id" : ObjectId(" 4bee804ba23d558eb6687120"), "x" : 6, "姓名" : "蔡花风10" }

{ "_id" : ObjectId("4befb711b0e29ba1ce20e0bc"), "姓名" : "1616", "信息" : { "url" : "http://www.1616.net/ ", "城市" : "北京" } }

  








{ "_id" : ObjectId("4befb723b0e29ba1ce20e0bd"), "名称" : " hao123", "info" : { "url" : "http://www.hao123.com/", "城市" : "北京" } }

 { "_id" : ObjectId("4befb740b0e29ba1ce20e0be "), "姓名" : "ll4la", "信息" : { "网址" : "http://www.114la.com/", "城市" : "东莞" } }

  "_id" : ObjectId("4befc51ab0e29ba1ce20e0c2"), "firstname" : "cai", "lastname" : "huafeng" }

db.data.ensureIndex({firstname: 1, lastname: 1} , {unique: true});

E11000 重复键错误索引:recommender.data.$firstname_1_lastname_1 dup key: { : null, : null }

接下来我们用另一个表person来测试

> db.person.ensureIndex({firstname:1, lastname: 1},{unique: true});

> db.person.insert({firstname: 'cai', lastname: 'huafeng'});

第二次插入相同值时,报错,提示唯一索引生效,其实和MySQL中是一样的。

<> db.person.insert({firstname:'cai', lastname:'huafeng'});

E11000 重复键错误索引:recommender.person.$firstname_1_lastname_1 dup key: { : "cai", : "huafeng" }

< p> 六、唯一索引中重复值处理

删除在上面5条索引中插入两行相同的记录

<> db.person.dropIndexes();

{

<"nIndexesWas" : 2,

“msg”:“为集合删除非_id索引”,

“ok”:1

}

<> db.person.find();

<{ "_id" : ObjectId("4befcda6b0e29ba1ce20e0cf"), "firstname" : "cai ", "lastname" : "huafeng " }

<> db.person.insert({firstname: 'cai', lastname: 'huafeng'});

< > db.person .find();

 { "_id" : ObjectId("4befcda6b0e29ba1ce20e0cf"), "名字" : "蔡", "姓氏" : "华峰" }

 { "_id " : ObjectId("4befcef0b0e29ba1ce20e0d1") , "firstname" : "cai", "lastname" : "huafeng" }

如果直接在firstname和lastname字段上创建唯一组合索引,肯定会报错。我们来尝试一下:

> db.person.ensureIndex({firstname: 1, lastname: 1}, {unique: true});

E11000重复键错误索引: suggester.person.$firstname_1_lastname_1 dup key: { : "cai", : "huafeng" }

查看表person的索引,可以看到新创建的索引没有生成。

<> db.person.getIndexes( );

<[

<{

<"name" : "_id_",

 "ns" : "推荐人.person",

 "key" : {

<"_id" : 1

<}

<}

<]

您可以添加dropDups: 第二个json对象为true,这样创建唯一组合索引时不会报错,文档中第一个重复值会被保留,其他重复值会被删除。

再次测试,添加dropDups选项,虽然报错,但唯一的组合索引已经创建完毕。

> db.person.ensureIndex({firstname: 1, lastname: 1}, { unique: true, dropDups: true});

E11000 重复键错误索引:recommender.person.$firstname_1_lastname_1 dup key: { : "cai", : "huafeng" }

★> db .person.getIndexes();

<[

<"name" : "_id_",

<"ns" : "推荐人.person",

: {

<"_id": 1

<}< /p>

<},

“_id”:ObjectId(“4befcfd9b0e29ba1ce20e0d3”),

“ns”:“recommender.person”,

“key”: {

​“名字”:1,

​“姓氏”:1

​},

​“姓名”:“firstname_1_lastname_1",

“唯一”:true,

“dropDups”:true

再次查询person表中的记录,发现重复的记录已经被自动删除了。

​> db.person.find();

​{ "_id" : ObjectId( "4befcda6b0e29ba1ce20e0cf"), "firstname" : "cai", "lastname" : "huafeng" }

​MongoDB官方文档说明

​不能在以下键上创建唯一索引:具有重复值。如果您仍想创建索引,保留第一个文档的数据库索引并删除所有具有重复值的后续文档,请添加 dropDups 选项。

:db.things.ensureIndex({ firstname : 1}, {unique : true , dropDups : true})

【7.删除索引

【a.删除表中所有索引

【删除】指定集合上的所有索引:

​db.collection.dropIndexes();

​b.删除表中的单个索引

​删除单个索引:

db.collection.dropIndex({x: 1, y: -1})

<> db.data.dropIndex({名字: 1, 姓氏: 1});

<{ "nIndexesWas" : 6, "ok" : 1 }

​不带帮助程序直接作为命令运行:

​ // 注意:在 MongoDB v1.3.2 之前,命令是 "deleteIndexes",而不是 "dropIndexes"

/ / 从集合 foo 中删除键模式为 {y:1} 的索引

:db.runCommand({dropIndexes:'foo', index : {y :1}})

//删除所有索引:

db.runCommand({dropIndexes:'foo', index : '*'})

 > db.person.ensureIndex({firstname: 1, lastname: 1});

< > db.runCommand({dropIndexes:'person', index:{名字:1, 姓氏:1 }});

<>{ "nIndexesWas" : 2 , "ok" : 1 }

上面的内容是如何在MongoDB数据库中使用索引,你已经学会了知识或者技巧吗?如果您想学习更多技能或者丰富自己的知识库,请关注行业资讯频道。

1. 本站所有资源来源于用户上传或网络,仅作为参考研究使用,如有侵权请邮件联系站长!
2. 本站积分货币获取途径以及用途的解读,想在本站混的好,请务必认真阅读!
3. 本站强烈打击盗版/破解等有损他人权益和违法作为,请各位会员支持正版!
4. 编程技术 > 如何在MongoDB数据库中使用索引

用户评论