Home > Software engineering >  How to convert document's values into an array using mongodb aggregration?
How to convert document's values into an array using mongodb aggregration?

Time:06-01

I need to achieve this simple thing. I have these 2 documents

1: {a="010", b="020", c="030"}
2: {a="030", b="040", c="050"}

I need to obtain the following document using mongodb aggregations:

{ result = [{a="010", b="020", c="030"}, {a="030", b="040", c="050"}] }

Thank you very much

CodePudding user response:

db.collection.aggregate([
{
   $group:{
     "_id": null,
     "result": {
      $push: "$$ROOT"
     }
   }
}

])

Group all the docs under result as an array.

  • Related