I'm using a version of mongodb that doesn't support $objectToArray
.
In later versions of mongodb I am successfully using the following snippet in an aggregation pipeline:
{
$project: {
_id: 1,
arr: {
$objectToArray: "$obj"
}
}
}
Other answers suggest using objectToArray
(sic) or scripting languages (python) or mongo solutions outside of the aggregation pipeline.
Is there a way to use map/reduce or something similar in earlier versions of mongo inside the aggregation pipeline?
Data input:
[
{
"id": "0",
"obj": {
"obj-0": {"prop": "value"},
"obj-1": {"prop": "value"}
}
}
]
Desired result:
[
{
"id": "0",
"arr": [
{"prop": "value"},
{"prop": "value"}
]
}
]