I'm using mongodb aggregation and added a facet
stage. Here is the output of this stage.
{
"a": [{x: 1}, {x: 2}],
"b": [{x: 3}, {x: 4}]
}
Now I want to get rid of the a
and b
keys and create a list of objects from it. So final result should be like the following:
[
{x: 1},
{x: 2},
{x: 3},
{x: 4}
]
What should I use in to aggregation pipeline for this?
CodePudding user response:
One option is:
db.collection.aggregate([
{$project: {_id: 0, data: {$concatArrays: ["$a", "$b"]}}}
])
See how it works on the playground example
If you want an array and not an object, add to it:
{$unwind: "$data"},
{$replaceRoot: {newRoot: "$data}}
See how it works on the playground example - no object