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