I have documents like this one at collection x
at MongoDB:
{
"_id" : ...
"attrKeys": [ "A1", "A2" ],
"attrs" : {
"A1" : {
"type" : "T1",
"value" : "13"
},
"A2" : {
"type" : "T2",
"value" : "14"
}
}
}
The A1
and A2
elements above are just examples: the attrs
field may hold any number of keys of any name. The key names in attrs
are stored in the attrNames
field.
I would like to query for documents which have an attr
with a sub-field of a given value. For example, a query for documents that have an element in the attr
key-map which sub-field type
is "T4". Something like this:
db.x.find({"attrs.$any.type": "T4"})
(The avobe is not legal MongoDB query language, but I think it could help to get the idea).
Is that query possible with MongoDB? Is there any workaround in the case MongoDB doesn't support that query? Thanks!
EDIT: original versions of the data model use an array for attrs
instead of a key-map. However, that changed in favour of a key-map in order to allow concurrent modifications on the same document.
I mean, using a key-map two independent clients can modify attrs
elements, as one client may do db.x.update({_id: "y"}, {$set: { "attrs.A1.value": "12" } }
and another client do db.x.update({_id: "y"}, {$set: { "attrs.A2.value": "55" } }
without interferring one each other.
In the case of using an array concurrent access is much more harder. Any hint on how it could be done?