Aishwarya
Aishwarya

Reputation: 33

Add new field to each array elements in Mongodb aggregation based on match condition

I have the following mongodb collection

 {  "id" : "yzes547",
    "courseId" : "aw4a34y7",
    "objectives" : [ 
        {
            "type" : "LESSON",
            "id" : "dfhxytd"
        }, 
        {
            "type" : "MODULE",
            "id" : "rgzeerp"
        }
    ]
}

I want to add new fields for ids of each type in objectives parameter.

Desired output

{  "id" : "yzes547",
    "courseId" : "aw4a34y7",
    "objectives" : [ 
        {
            "type" : "LESSON",
            "id" : "dfhxytd"
        }, 
        {
            "type" : "MODULE",
            "id" : "rgzeerp"
        }
    ]
   "lessonId": "dfhxytd",
   "moduleId": "rgzeerp"
}

I have tried the following query but it only returns False.

db.account.aggregate([
{
    '$project': {
        '_id': 0, 
        'questionId': 1, 
        'courseId': 1, 
        'lessonId': {
            '$cond': [
                {
                    '$eq': [
                        '$objectives.type', 'LESSON'
                    ]
                }, '$objectives.id', False
            ]
        }, 
        'moduleId': {
            '$cond': [
                {
                    '$eq': [
                        '$objectives.type', 'MODULE'
                    ]
                }, '$objectives.id', False
            ]
        }

Is it possible to query the database such that we can get new fields of each type in one aggregation method?

Upvotes: 2

Views: 898

Answers (1)

Tom Slabbaert
Tom Slabbaert

Reputation: 22276

Yes, you can achieve this quite easily using $map and $objectToArray to create the required fields, finally by using $replaceRoot and $mergeObjects you can add these fields to the original structure, like so:

db.collection.aggregate([
  {
    $replaceRoot: {
      newRoot: {
        "$mergeObjects": [
          "$$ROOT",
          {
            "$arrayToObject": {
              $map: {
                input: "$objectives",
                as: "obj",
                in: {
                  k: {
                    $concat: [
                      {
                        "$toLower": "$$obj.type"
                      },
                      "Id"
                    ]
                  },
                  v: "$$obj.id"
                }
              }
            }
          }
        ]
      }
    }
  }
])

Mongo Playground

Upvotes: 2

Related Questions