1

I have a MongoDB object like:

{

    "courseName": "ML",
    "user": ObjectId("6087dc4c2ba7a828363c9fca"),
    "questions": [  
        {

            "topics": [
               "a","b"
            ],
        },
        {
            "topics": [
                "a","b","d"
            ],
        }
    ]
}

I want to find the questions with courseName and user into questions, I want to remove the topic(s) that matches with the input topic.

How can I do this?

If my input is ["a"] then the update output should look like:

{

    "courseName": "ML",
    "user": ObjectId("6087dc4c2ba7a828363c9fca"),
    "questions": [  
        {

            "topics": [
               "b"
            ],
        },
        {
            "topics": [
                "b","d"
            ],
        }
    ]
}
0

1 Answer 1

2
  • $[] positional for all elements in questions and check condition by $in operator
db.collection.updateMany(
  { 
    "courseName": "ML",
    "user": ObjectId("6087dc4c2ba7a828363c9fca"),
    "questions.topics": { $in: ["a"] } 
  },
  {
    $pull: {
      "questions.$[].topics": { $in: ["a"] }
    }
  }
)

Playground

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

Comments

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.