2

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?

1 Answer 1

2

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

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

1 Comment

mongoDB is a maze of curly braces. Oh! God.

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.