加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_阳江站长网 (https://www.0662zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 政策 > 正文

MongoDB通配符索引的用法实例

发布时间:2020-10-15 21:13:58 所属栏目:政策 来源:做站长
导读:这篇文章主要给大家介绍了关于MongoDB通配符索引的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 指南 MongoDB在4.2 版本推出了Wildcard Indexes,究竟什么是Wildcar

这篇文章主要给大家介绍了关于MongoDB通配符索引的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

指南

MongoDB在4.2 版本推出了Wildcard Indexes,究竟什么是Wildcard Indexes以及Wildcard Indexes适合哪些场景本文结合官方文档以及实际测试进行简单概述。

1、通配符索引示例

因为MongoDB是dynamic schemas,所以应用是可以查询任何已知字段或者随机字段的。

假设(此假设案例摘自官方文档),集合colA的UserMetadata字段包含如下数据:

{ “userMetadata” : { “likes” : [ “dogs”, “cats” ] } }
{ “userMetadata” : { “dislikes” : “pickles” } }
{ “userMetadata” : { “age” : 45 } }
{ “userMetadata” : “inactive” }

但是在查询的时候可能是如下语句:

db.colA.find({ “userMeta2
通配符索引的形式data.likes” : “dogs” })
db.colA.find({ “userMetadata.dislikes” : “pickles” })
db.colA.find({ “userMetadata.age” : { $gt : 30 } })
db.colA.find({ “userMetadata” : “inactive” })

是否能通过一个索引来完成上述需求?

答案是肯定的,上述查询可以通过通配符索引来实现既定需求,也就是 db.colA.createIndex( { “userMetadata.$**” : 1 } )。

那么如何创建通配符索引?

注意: 首先应该明确的是通配符索引只在版本兼容性4.2的时候才能创建。

如何查询版本兼容性?

db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )

如何设置?

db.adminCommand( { setFeatureCompatibilityVersion: “4.2” } )

2、通配符索引的形式

单字段通配符索引

{
 “_id” : ObjectId(“5ee2df16911d8dfaa91520b4”),
 “product_name” : “Spy Coat”,
 “product_attributes” : {
 “material” : [
 “Tweed”,
 “Wool”,
 “Leather”
 ],
 “size” : {
 “length” : 72,
 “units” : “inches”
 }
 }
}
{
 “_id” : ObjectId(“5ee2df30911d8dfaa91520b5”),
 “product_name” : “Spy Pen”,
 “product_attributes” : {
 “colors” : [
 “Blue”,
 “Black”
 ],
 “secret_feature” : {
 “name” : “laser”,
 “power” : “1000”,
 “units” : “watts”
 }
 }
}

如果数据结构是上面这样的,其中product_attributes 属性包含任何的结构。

那么如果我们创建一个这个索引,它会进行什么操作?

db.product_catalog.createIndex({“product_attributes.$**”:1})。

因为product_attributes 里面包含数组和嵌套文档等对象,实际创建这个索引后,会迭代嵌套文档或者数组把里面的所有的值都取出来放到索引里。支持如下查询:

db.product_catalog.find({“product_attributes.colors”:”Blue”})
db.product_catalog.find({“product_attributes.secret_feature.name”:”laser”})
db.product_catalog.find({“product_attributes.size.length”:{$gt:60}})

全字段的通配符索引

可以通过下面的语句创建一个索引,索引中包含集合中的所有字段,但是不包括_id(如果想包含_id可以通过wildcardProjection 来设置),如果集合中的字段包含数组或者嵌套对象的话,那么会迭代数组或者嵌套对象并把值放到索引中。

Db.product_catalog.createIndex({“$**”:1})

给每个文档添加一个address的字段。

7777:PRIMARY> db.product_catalog.find().pretty()
{
 “_id” : ObjectId(“5ee2df16911d8dfaa91520b4”),
 “product_name” : “Spy Coat”,
 “product_attributes” : {
 “material” : [
 “Tweed”,
 “Wool”,
 “Leather”
 ],
 “size” : {
 “length” : 72,
 “units” : “inches”
 }
 },
 “address” : “Beijing”
}
{
 “_id” : ObjectId(“5ee2df30911d8dfaa91520b5”),
 “product_name” : “Spy Pen”,
 “product_attributes” : {
 “colors” : [
 “Blue”,
 “Black”
 ],
 “secret_feature” : {
 “name” : “laser”,
 “power” : “1000”,
 “units” : “watts”
 }
 },
 “address” : “Tianjin”
}

db.product_catalog.find({“product_name”:”Spy Coat”,”address”:”nanji”,”product_attributes.colors”:”Blue”})

在全字段通配符索引的基础上可以明确包含哪些或者不包含哪些字段到通配符索引中,只能是在全字段通配符索引的基础上,单字段的是不可以的:

在全字段的基础上创建一个明确包含哪些字段的索引:

(编辑:应用网_阳江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读