How to make a query in this nested document structure (MongoDB)?

有些话、适合烂在心里 提交于 2019-12-10 09:17:03

问题


(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']

回答1:


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



来源:https://stackoverflow.com/questions/7723620/how-to-make-a-query-in-this-nested-document-structure-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!