How to make a request in this subdocument structure (MongoDB)?

(Sorry if this is a trivial question.)

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

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

In this structure, it is easy to find all documents containing "featureX" in the list of functions. But I'm also interested in getting the value associated with the sub. I think in Python, if I get a document with this request: db.articles.find({'features.id': 'featureX'}) , then I will need to db.articles.find({'features.id': 'featureX'}) over the "functions" of the array to find out the correct "value" .

Is there any other query that can give me an interesting value (in this case, I do not need to retrieve the full document, but only the part with {'id': 'featureX', 'value': 6}, which will not have a predictable index value in an array.

PS: I think that I will develop the document in different ways, but I'm still interested to know if re-execution is possible.

Here is the new structure:

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

Are there any problems with this desgin? In my case, "featureX", "featureY" are unique, so using them as dictionary keys is not a problem.

Edit: I need to clarify that I will need to update atomically "featureX" and "featureY" in the document and MongoDB does not support transactions . In addition, the second design, not allowing to extract the subdocument, should simplify the receipt of the desired information in the client code, assuming that I can request subdocuments that have a specific key.

I think this request should complete the task:

 result = db.articles.find_one({ 'features.featureX': {'$exists': True} } ) interesting_value = result['features']['featureX']['value'] 
+4
source share
1 answer

I’ve talked a couple of times about collecting sub-documents from the mongo collection here , and here

There is simply no way to do it now. This is the filtering behavior of a multi-level embedded document, usually the corresponding filter returns the entire document, not subsets.

There are two unresolved issues in mongo related to this positional ($) operator in the fields to return the specifier and the Ability to use subdocument data whose contents were used to satisfy the request using the $ operator . (Please login to vote if you really need this feature)

And your alternative circuit is also not useful here.

therefore, you need to save each function in a separate document so that it works the way you want.

function 1

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

function 2

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

and request

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

returns only a specific function

+2
source

All Articles