如何在这个嵌套文档结构(MongoDB)中进行查询? [英] How to make a query in this nested document structure (MongoDB)?

查看:352
本文介绍了如何在这个嵌套文档结构(MongoDB)中进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(对不起,这是一个微不足道的问题。)



我有这样的文件(Python语法):




{'id':'featureX','value':
$' 6},
{'id':'featureY','value':45}
]
}

使用此结构,可以轻松地在功能列表中找到包含featureX的所有文档。但是我也有兴趣检索子文档中关联的值。
我认为在Python中,如果我得到一个这样的查询的文档: db.articles.find({'features.id':'featureX'})那么我需要遍历数组的特征来找出正确的值。



是否有其他类型的查询可以给我有趣的价值(在这种情况下,我不需要检索完整的文档,只有具有{' id':'featureX','value':6},它不会是数组中可预测的索引值。



PS:我想我会设计该文件不同,但我仍然有兴趣知道上述可能的价格是否可行。



以下是新结构:

  {
'_id':SomeObjectId,
'features':{
'featureX':{'someproperty':'aaa' ,'value':6},
'featureY':{'someproperty':'bbb','value':45}
}
}

有没有关于这个设计的问题?
在我的情况下,featureX,featureY是唯一的,所以使用它们作为词典键不是一个问题。



编辑:
我需要澄清一点,我将需要更新原子地说featureX和featureY在文档中, a href =https://jira.mongodb.org/browse/SERVER-2804 =nofollow> MongoDB不支持事务。
另外,第二个设计虽然不允许检索一个子文档,但应该可以很容易地获得客户端代码中所需的信息,假设我可以查询具有某个密钥的子文档。



我认为这个查询应该做这个工作:

  result = db.articles .find_one({'features.featureX':{'$ exists':True}})
interesting_value = result ['features'] ['featureX'] ['value']


解决方案

我已经回答了这几次关于从mongo集合中单独提取子文档<一个href =http://stackoverflow.com/questions/7692865/mongodb-querying-array-elements-withina-a-document/7702608#7702608>这里和其中



只是目前无法做到这一点。这是过滤多级嵌入式文档的行为,通常匹配过滤器将返回整个文档,而不是子集。



在mongo中有两个尚未解决的问题此返回说明符的字段中的位置($)运算符能够使用其内容用于使用$运算符来满足查询的子文档的数据。 (如果您真的需要此功能,请登录进行投票)



而您的备用架构在此也无效。



所以您必须将这些功能存储在单独的文档中,以使其按照您想要的方式工作。



功能1


$ b $ a

'_id':SomeObjectId,
'name':'some name',
'value':'feature 1'
'some_field':'zzz'
}

功能2

  {
'_id':SomeObjectId,
'name':'some name',
'值':'特征2',
'some_field':'zzz'
}

并查询

  db.features.find({'_ id':someobjectid})

只会返回特定功能




(Sorry if it's a trivial question.)

I have documents that looks like this (Python syntax):

{
  '_id': SomeObjectId,
  'features': [ 
                {'id': 'featureX' , 'value': 6},
                {'id': 'featureY', 'value': 45}
              ]
}

With this structure it's easy to find all documents that contains 'featureX' in the list of features. But I'm also interested in retrieving the value associated in the sub-document. I think in Python if I get the document with a query like this: db.articles.find({'features.id': 'featureX'}) then I would need to iterate over the array 'features' to find out the correct 'value'.

Is there an other kind of query that can give me the interesting value (in this case I don't need to retrieve the full document, only the part with {'id': 'featureX', 'value': 6}, which won't be at a predictable index value in the array.

PS: I think I will design the document differently, but I'm still interested to know if the resquest above is feasible.

Here is the new structure:

{
'_id': SomeObjectId,
'features': { 
              'featureX': { 'someproperty':'aaa', 'value':6 }, 
              'featureY': {'someproperty' :'bbb', 'value':45} 
            }
}

Is there any issue about this desgin ? In my case 'featureX', 'featureY' are unique so using them as dictionnary keys is not a problem.

Edit: I need to clarify that I will need to update atomically say 'featureX' and 'featureY' in a document and MongoDB does not support transactions. Also the second design, while not allowing to retrieve a subdocument, should makes it easy to get quickly the desired information in the client code, assuming that I can query for sub-documents having a certain key.

I think that this query should do the job:

result = db.articles.find_one({ 'features.featureX': {'$exists': True} } )
interesting_value = result['features']['featureX']['value']

解决方案

I have answered this couple of times about picking up the sub-documents alone from mongo collection here, and here

Simply there is no way to do this currently. This is the behavior of filtering multi level embedded document, normally the matching filter would return the whole document, not the subsets.

There are two outstanding issues already in mongo related to this positional ($) operator in fields to return specifier and Ability to make use of a subdocument's data whose contents were used to satisfy a query using the $ operator. (Please login to vote if you really needed the feature)

And your alternate schema also not useful here.

so you have to store the each feature in separate document like this to make it work the way you wanted

feature 1

{
'_id': SomeObjectId,
'name' :'some name',
'value': 'feature 1',
'some_field' : 'zzz'
}

feature 2

{
'_id': SomeObjectId,
'name' :'some name',
'value': 'feature 2',
'some_field' : 'zzz'
}

and querying

db.features.find({'_id':someobjectid})

will only return the specific feature

这篇关于如何在这个嵌套文档结构(MongoDB)中进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆