0

I have a collection called 'A' with thousands of documents in it. Here grades is the array of sub documents which consist attribute questions which is array of number like below

{ "_id" : 1,
      "grades" : [
        { type: "quiz", questions: [ 10, 8, 5 ] },
        { type: "quiz", questions: [ 8, 9, 6 ] },
        { type: "hw", questions: [ 5, 4, 3 ] },
        { type: "exam", questions: [ 25, 10, 23, 0 ] }
      ]
   }

In order to add additional feature we have to modify the format of questions and make it array of documents which has two fields one is number (which will inherit the old data) and another one is attempts(by default it will be empty array and may have value 'P'- Pass or 'F'- Fail). After this transformation the document should look like below

{ "_id" : 1,
      "grades" : [
        { 
          type: "quiz",
          questions:[ 
                      { number: 10, attempts: ['F', 'P'] },
                      { number: 8, attempts: [] },
                      { number: 5, attempts: ['P'] }
                    ] 
        },
        { 
          type: "quiz", 
          questions: 
                    [ 
                     { number: 8, attempts: ['F', 'P'] },
                     { number: 9, attempts: [] },
                     { number: 6, attempts: ['P'] }
                    ] 
        },
        { 
          type: "hw",
          questions: 
                    [ 
                       { number: 5, attempts: ['F', 'P'] },
                       { number: 4, attempts: [] },
                       { number: 3, attempts: ['P'] }
                    ]
        },
        {
           type: "exam",
           questions: 
                     [ 
                       { number: 25, attempts: ['F', 'P'] },
                       { number: 10, attempts: [] },
                       { number: 23, attempts: ['P'] },
                       { number: 0, attempts: ['P', 'P'] }
                     ] 
        }
      ]
   }

Appreciate for all the help

1 Answer 1

1

You can test yourself here: https://mongoplayground.net/p/ZEp_vk5hhf7

Brief explanation:
You have to use $map two times.

  • First you have to loop the $grades field, because he is an array.
  • As you are looping through $grades array, you have to loop through the $questions field, because he is an array too.

Query:

db.collection.aggregate([
  {
    "$project": {
      "grades": {
        "$map": {
          "input": "$grades",
          "as": "g",
          "in": {
            "type": "$$g.type",
            "questions": {
              "$map": {
                "input": "$$g.questions",
                "as": "q",
                "in": {
                  "number": "$$q",
                  "attempts": []
                }
              }
            }
          }
        }
      }
    }
  }
])

Result:

[
  {
    "_id": 1,
    "grades": [
      {
        "questions": [
          {
            "attempts": [],
            "number": 10
          },
          {
            "attempts": [],
            "number": 8
          },
          {
            "attempts": [],
            "number": 5
          }
        ],
        "type": "quiz"
      },
      {
        "questions": [
          {
            "attempts": [],
            "number": 8
          },
          {
            "attempts": [],
            "number": 9
          },
          {
            "attempts": [],
            "number": 6
          }
        ],
        "type": "quiz"
      },
      {
        "questions": [
          {
            "attempts": [],
            "number": 5
          },
          {
            "attempts": [],
            "number": 4
          },
          {
            "attempts": [],
            "number": 3
          }
        ],
        "type": "hw"
      },
      {
        "questions": [
          {
            "attempts": [],
            "number": 25
          },
          {
            "attempts": [],
            "number": 10
          },
          {
            "attempts": [],
            "number": 23
          },
          {
            "attempts": [],
            "number": 0
          }
        ],
        "type": "exam"
      }
    ]
  }
]
Sign up to request clarification or add additional context in comments.

1 Comment

igorkf, this works fine but this is just projection. I would like to update the questions field like [{ "attempts": [], "number": 8 }]

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.