The output of the db.name.aggregate() function gives output:
[{}, {"abc": "zyx"}, {}, "opk": "tyr"]
Actual output desired :
[{"abc": "zyx"}, "opk": "tyr"]
The output of the db.name.aggregate() function gives output:
[{}, {"abc": "zyx"}, {}, "opk": "tyr"]
Actual output desired :
[{"abc": "zyx"}, "opk": "tyr"]
2
Answers
Firstly, your output is not a valid array. It should be like this:
Now, to obtain your desired output, you can add the following
$match
stage, toyour pipeline:
Here, we are converting the document to an array using
$objectToArray
, and then we check whether the size of that array is greater than 0. Only those documents are kept in the output.Playground link.
What if you data looks like this.
The aggregation be like this to remove empty objects
Outputs
Demo@mongoplayground
https://mongoplayground.net/p/BjGxzlrlj6s