2

I'm trying to merge objects of an array in the aggregation pipeline. Here is MongoDB Document.

{
    "category" : [
        {
            "id" : ObjectId("60ed3d041436f1310401b1b2"),
            "name" : "cat 1",
            "services" : [
                {
                    "_id" : ObjectId("607ab64bc520c2587045694b"),
                    "name" : "service 1",
        
                }
            ]
        }
    ]
},

{
    "category" : [
        {
            "id" : ObjectId("60ed3cff1436f1310401b1b1"),
            "name" : "cat 2",
            "services" : [
                {
                    "_id" : ObjectId("5ffd5d4bc4967a1b10bf5022"),
                    "name" : "service 2",
                
                }
            ]
        },
        {
            "id" : ObjectId("60ed3cff1436f1310401b1b1"),
            "name" : "cat 2",
            "services" : [
                {
                    "_id" : ObjectId("6077e777ad481470d42c4e41"),
                    "name" : "service 3",
                    
                }
            ]
        }
    ]
}

Expected Output I need to merge the category array into one and merge the service array based on the category

     [
        {
            "id" : ObjectId("60ed3d041436f1310401b1b2"),
            "name" : "cat 1",
            "services" : [
                {
                    "_id" : ObjectId("607ab64bc520c2587045694b"),
                    "name" : "service 1",
        
                }
            ]
        },
        {
            "id" : ObjectId("60ed3cff1436f1310401b1b1"),
            "name" : "cat 2",
            "services" : [
                {
                    "_id" : ObjectId("5ffd5d4bc4967a1b10bf5022"),
                    "name" : "service 2",
                
                },
                {
                    "_id" : ObjectId("6077e777ad481470d42c4e41"),
                    "name" : "service 3",
                    
                }
            ]
        },
    ]

How to achieve this ?.

2
  • Not possible! You cannot have just an array inside an object. Commented Sep 20, 2021 at 13:40
  • @WernfriedDomscheit Sorry I have updated the question. Commented Sep 20, 2021 at 13:44

1 Answer 1

2

Try this one:

db.collection.aggregate([
  { $unwind: "$category" },
  {
    $group: {
      _id: {
        _id: "$_id",
        name: "$category.name"
      },
      services: { $push: { $first: "$category.services" } }
    }
  },
  {
    $project: {
      _id: "$_id._id",
      name: "$_id.name",
      services: 1
    }
  }
])

See Mongo Playground

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

6 Comments

I got an error .MongoError: Unrecognized expression '$first'
Which mongo version do you use? Try { $arrayElemAt: [ "$category.services", 0 ] } instead.
mongoDB version "4.2.2"
Could you please explain the complete code by using $arrayElemAt
Should be obvious: { $first: "$category.services" } -> { $arrayElemAt: [ "$category.services", 0 ] }
|

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.