1

I want to unwind two arrays in a single document. Now I want to project them separately as mentioned below with extra field that defined its type. Suppose if it is washed then the type should be defined as wash and If it is installed then the type should be defined as installed

The document is in the following format:

    {
"install":["Date1"]
"wash": ["Date1", "Date2", "Date3"]
}

The output I want is as follows:

{
"install": "Date1"
"type": "install"
},
{
"wash": "Date1",
"type": "wash"
},
{
"wash": "Date2",
"type": "wash"
},
{
"wash": "Date3",
"type":"wash"
}

1 Answers1

0

This aggregation returns the desired result:

db.test.aggregate( [
  { 
      $facet: {
          install: [
              { $unwind: "$install" },
              { $project: { type: "install", install: 1, _id: 0 } }
          ],
          wash: [
              { $unwind: "$wash" },
              { $project: { type: "wash", wash: 1, _id: 0 } }
          ]
      } 
  },
  {
      $project: { result: { $concatArrays: [ "$install", "$wash" ] } }
  }
] )
prasad_
  • 12,755
  • 2
  • 24
  • 36