6

I had the following array of structure in my aggregation pipeline. Tried merge objects and setUnion operators.

{ 
  combs:[
    [
      {
        name:"A",
        c_type:"A"
      },
      {
        type:"visual",
        severity:"Normal"
      }
    ],
    [
      {
        name:"B",
        c_type:"B"
      },
      {
        type:"visual",
        severity:"Normal"
      }
    ]
  ]
}

I am expecting the following results to produce some statistics. Please help me.

{ 
  combs:[
    {
      name:"A",
      c_type:"A",
      type:"visual",
      severity:"Normal"
    }
    {
      name:"B",
      c_type:"B",
      type:"visual",
      severity:"Normal"
     }
  ]
}

2 Answers 2

4

"Is it possible to achieve without $unwind operation?"

Well YES. As long as your structure of arrays of arrays is consistently mapped that way then you really only need a single stage in the pipeline:

db.collection.aggregate([
  { "$addFields": {
     "combs": {
       "$map": {
         "input": "$combs",
         "in": { "$mergeObjects": "$$this" }
       }
     }
  }}
])

So really the $map operator takes place here as a much more efficient method than $unwind for processing each array element. Also since $mergeObjects is expecting "an array of objects", this is what each element of your array of arrays actually is. So simply { "$mergeObjects": "$$this" } on each outer member of the array.

Produces the output from your supplied data:

{
        "_id" : ObjectId("5d8865c273375a6a4cc9e76a"),
        "combs" : [
                {
                        "name" : "A",
                        "c_type" : "A",
                        "type" : "visual",
                        "severity" : "Normal"
                },
                {
                        "name" : "B",
                        "c_type" : "B",
                        "type" : "visual",
                        "severity" : "Normal"
                }
        ]
}

Generally you should always prefer an inline processor like $map or other array operators in preference to $unwind where applicable.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use this aggregation query

db.collection.aggregate([
     { $unwind: "$combs" },
     { $addFields: { combs: { $mergeObjects: "$combs" }}},
     { $group: { _id: "$_id", combs: { $push: "$combs" }} }
 ])

1 Comment

Is it possible to achieve without $unwind operation? @Ashok

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.