I have two documents (obtained by other steps in an aggregation pipeline):
{
'_id': '2021-01-04',
'value': 1234.55
},
{
'_id': '2021-01-05',
'value': 345.67
}
I would now like to convert these two documents into an array that would look like this:
[
{ '2021-01-04': 1234.55 },
{ '2021-01-05': 345.67 }
]
I've tried to first convert the key/value pairs using a $group stage like so:
$group: {
_id: null,
data: {
$push: {
"k": "$_id",
"v": "$value"
}
}
}
This yields:
[
{
"_id": null,
"data": [
{
"k": "2019-01-04",
"v": 1234.55
},
{
"k": "2019-01-05",
"v": 345.67
}
]
}
]
While this would be useful as input for $arrayToObject, I don't want an object (as I need the objects to be ordered), but I cannot see how to get from here to the desired final output.