Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert deeply nested array with one object to object in mongodb aggregation?

The requirement is to convert all sales array properties to objects with their first elements’ quantity value. The collection name is branches and the example data is below.

[
{
    "_id": 111,
    "recs": [
        {
            "date": "01-01-2021",
            "sales": [
                {
                    "quantity": {
                        "P": 14,
                        "K": 0,
                        "U": 0
                    }
                }
            ]
        },
        {
            "date": "02-01-2021",
            "sales": [
                {
                    "quantity": {
                        "P": 23,
                        "K": 0,
                        "U": 0
                    }
                }
            ]
        }
    ]
},
{
    "_id": 21,
    "recs": [
        {
            "date": "01-01-2021",
            "sales": [
                {
                    "quantity": {
                        "P": 11,
                        "K": 0,
                        "U": 0
                    }
                }
            ]
        },
        {
            "date": "02-01-2021",
            "sales": [
                {
                    "quantity": {
                        "P": 31,
                        "K": 0,
                        "U": 0
                    }
                }
            ]
        }
    ]
}]

The expected response is below.

[
{
    "_id": 111,
    "recs": [
        {
            "date": "01-01-2021",
            "sale": {
                "P": 14,
                "K": 0,
                "U": 0
            }
        }
    ]
}]

I tried $map, $addFields but no way I couldn’t find a solution.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In $map, you need:

  1. $first to query the first sales.quantity object in recs.sales array.
  2. $mergeObject to merge the current document in recs array with the document from (1).
db.collection.aggregate([
  {
    "$project": {
      _id: 1,
      recs: {
        "$map": {
          "input": "$recs",
          "in": {
            "$mergeObjects": [
              "$$this",
              {
                sales: {
                  $first: "$$this.sales.quantity"
                }
              }
            ]
          }
        }
      }
    }
  }
])

Sample Mongo Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading