2

I am working on MongoDb aggregation. I am new for mongo pipelines and stuck on some point. I have documents in MongoDB collection like below. I am unable to guess how to move forward to get expected result:

{
    resource: {
        name: "PROJECT_01", 
        version: 1,
        owner: ""
        },
    app_info: [
        {
        app_key: "APP_01",
        size: 20,
        metadata:{
            deploy_on: "AWS",
            status: "running",
            reason:{}
            }
        },
        {
        app_key: "APP_01",
        size: 20,
        metadata:{
            deploy_on: "azure",
            status: "failed",
            reason:{
                message: "Connectivity Issue",
                error_code: "CONNECTIVITY_ERROR"
                }
            }
        },
        {
        app_key: "APP_02",
        size: 20,
        metadata:{
            deploy_on: "AWS",
            status: "running",
            reason:{}
            }
        },
        {
        app_key: "APP_02",
        size: 20,
        metadata:{
            deploy_on: "azure",
            status: "failed",
            reason:{
                message: "Connectivity Issue",
                error_code: "CONNECTIVITY_ERROR"
                }
            }
        },
    ]
}

I want to combine metadata on bases of "app_key" by using aggregation and expected output should be like this:

{
    resource: {
        name: "PROJECT_01", 
        version: 1,
        owner: ""
        },
    app_info: [
        {
        app_key: "APP_01",
        size: 20,
        metadata:[{
            deploy_on: "AWS",
            status: "running",
            reason:{}
            },
            {
            deploy_on: "azure",
            status: "failed",
            reason:{
                message: "Connectivity Issue",
                error_code: "CONNECTIVITY_ERROR",
                }
            }
        ]},
        {
        app_key: "APP_02",
        size: 20,
        metadata:[{
            deploy_on: "AWS",
            status: "running",
            reason:{}
            },
            {
            deploy_on: "azure",
            status: "failed",
            reason:{
                message: "Connectivity Issue",
                error_code: "CONNECTIVITY_ERROR"
                }
            }
        ]}
}

If someone have idea, Please share here. Thanks.

1 Answer 1

1

Welcome to mongo db, You can try something like following.

  • $unwind to deconstruct the array
  • $group to reconstruct the array for the expected format. $first helps to get the first element of the documents. $addToSet helps to remove if there any duplicates while phusing into an array. You can use $push
  • $addFields to get the expected format.

Script is..

db.collection.aggregate([
  {
    $unwind: "$app_info"
  },
  {
    $group: {
      _id: {
        app_key: "$app_info.app_key",
        resource: "$resource"
      },
      size: {
        $first: "$app_info.size"
      },
      metadata: {
        $push: "$app_info.metadata"
      },
      resource: {
        $first: "$resource"
      }
    }
  },
  {
    $group: {
      _id: "$_id.resource",
      appInfos: {
        $push: {
          app_key: "$_id.app_key",
          size: "$size",
          metadata: "$metadata"
        }
      }
    }
  }
])

Working Mongo playground

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

6 Comments

Hi @varman, It is working well for single document or for single resource but failing for multiple documents with different resource. Mongo Playground. It is adding all app_info in single document.
what is the expected output for above scenario?
I think you missed group by resource, i just answered here
@turivishal might be. Thanks my frnd,
@varman i just seen this similar answer and informed you :), you can update your answer i don't mind. i am not going to answer again.
|

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.