如何在这种嵌套文档结构(MongoDB)中进行查询?
(抱歉,如果这是一个微不足道的问题。)
我的文档看起来像这样(Python 语法):
{
'_id': SomeObjectId,
'features': [
{'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”以及 MongoDB 不支持事务。 此外,第二种设计虽然不允许检索子文档,但应该可以轻松地在客户端代码中快速获取所需的信息,假设我可以查询具有特定键的子文档。
我认为这个查询应该完成这项工作:
result = db.articles.find_one({ 'features.featureX': {'$exists': True} } )
interesting_value = result['features']['featureX']['value']
(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']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经回答过几次关于从 mongo 集合中单独获取子文档的问题 这里,以及此处
目前还没有办法做到这一点。这是过滤多级嵌入文档的行为,通常匹配过滤器会返回整个文档,而不是子集。
mongo 中已经存在两个与此相关的未决问题 返回说明符的字段中的位置 ($) 运算符< /a> 和 能够利用子文档的数据内容用于满足使用 $ 运算符 的查询。 (如果您确实需要该功能,请登录投票)
并且您的备用架构在这里也没有用。
因此,您必须将每个功能存储在单独的文档中,如下所示,以使其按照您想要的
功能 1
功能 2
的方式工作,并且查询
将仅返回特定功能
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
feature 2
and querying
will only return the specific feature