I have the current aggregation output as follows:
[
{
"courseCount": 14
},
{
"registeredStudentsCount": 1
}
]
The array has two documents. I would like to combine all the documents into a single document having all the fields in mongoDB
2
Answers
When you add this as the last pipeline, it will put all the docs under data, but here data would be an array of objects.
In your case it would be
Another approach would be,
It’s not dynamic as first one. As you have two docs, you can use this approach. It outputs
With extra pipeline in the second approach
You will get the output as
Explained:
Plyaground