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.