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 use the MongoDB aggregation framework to transform that document into one like this:
{
"_id" : ...
"attrs" : [
{
"key": "A1",
"type" : "T1",
"value" : "13"
},
{
"key": "A2",
"type" : "T2",
"value" : "14"
}
]
}
That is, to become attrs into an array, which elements are the same that the key values "passing" the key into a new field inside each array element of name key.
It is possible use the aggregation framework for suck transformation? I tend to think that $project operator could be used, but I haven't figured out how.
attrsand it is perfectly known. Another issue is that I need using keys insideattrsin order to process concurrent updates on attributes, e.g.{$set: {attrs.A1: {...}}}and{$set: {attrs.A2: {...}}}which could be very difficult to manage storing attributes as an array. - fgalanattrKeysfield. I have edited the question to include that information (I haven't included in my original post thinkingattrKeyswas not meaniful to solve the question, sorry for that). - fgalan