1

This is my data I want to remove this specific object only:
thanks to advance how to remove the specific object from a nested array element

{ "_id" : ObjectId("5397eb925d2664177b0fc5a6"), "answer" : "Item Question 1 - Answer 1", "isCorrectAnswer" : true }

Input data:

   {
        "_id" : ObjectId("5397e4b75c4c9bf0509709ab"),
        "name" : "Item Name",
        "description" : "Item Description",
        "questions" : [
            {
                "_id" : ObjectId("5397eb925d2664177b0fc5a5"),
                "question" : "Item Question 1",
                "answers" : [
                        {
                            "_id" : ObjectId("5397eb925d2664177b0fc5a6"),
                            "answer" : "Item Question 1 - Answer 1",
                            "isCorrectAnswer" : true
                        },
                        {
                            "_id" : ObjectId("5397eb925d2664177b0fc5a7"),
                            "answer" : "Item Question 1 - Answer 2",
                            "isCorrectAnswer" : false
                        }
                    
                    ]
            },
            {
                "_id" : ObjectId("5397eb925d2664177b0fc5a9"),
                "question" : "Item Question 2",
                "answers" : [
                    {
                        "_id" : ObjectId("5397eb925d2664177b0fc5aa"),
                        "answer" : "Item Question 2 - Answer 1",
                        "isCorrectAnswer" : false
                    },
                    {
                        "_id" : ObjectId("5397eb925d2664177b0fc5ab")
                        "answer" : "Item Question 2 - Answer 2",
                        "isCorrectAnswer" : true
                    }
                ]
            }
        ]
    }

expected output:
   {
        "_id" : ObjectId("5397e4b75c4c9bf0509709ab"),
        "name" : "Item Name",
        "description" : "Item Description",
        "questions" : [
            {
                "_id" : ObjectId("5397eb925d2664177b0fc5a5"),
                "question" : "Item Question 1",
                "answers" : [
                       
                        {
                            "_id" : ObjectId("5397eb925d2664177b0fc5a7"),
                            "answer" : "Item Question 1 - Answer 2",
                            "isCorrectAnswer" : false
                        }
                    ]
            },
            {
                "_id" : ObjectId("5397eb925d2664177b0fc5a9"),
                "question" : "Item Question 2",
                "answers" : [
                    {
                        "_id" : ObjectId("5397eb925d2664177b0fc5aa"),
                        "answer" : "Item Question 2 - Answer 1",
                        "isCorrectAnswer" : false
                    },
                    {
                        "_id" : ObjectId("5397eb925d2664177b0fc5ab")
                        "answer" : "Item Question 2 - Answer 2",
                        "isCorrectAnswer" : true
                    }
                ]
            }
        ]
    }

thanks to advance how to remove the specific object from a nested array element

1 Answer 1

1

Solution in Mongoplayground. Try this:

db.collection.updateOne(
    { "_id": ObjectId("5397e4b75c4c9bf0509709ab") },
    {
        $pull: {
            "questions.$[].answers": {
                "_id": ObjectId("5397eb925d2664177b0fc5a6")
            }
        }
    }
);

Output:

{
    "_id" : ObjectId("5397e4b75c4c9bf0509709ab"),
    "name" : "Item Name",
    "description" : "Item Description",
    "questions" : [
        {
            "_id" : ObjectId("5397eb925d2664177b0fc5a5"),
            "question" : "Item Question 1",
            "answers" : [
                {
                    "_id" : ObjectId("5397eb925d2664177b0fc5a7"),
                    "answer" : "Item Question 1 - Answer 2",
                    "isCorrectAnswer" : false
                }
            ]
        },
        {
            "_id" : ObjectId("5397eb925d2664177b0fc5a9"),
            "question" : "Item Question 2",
            "answers" : [
                {
                    "_id" : ObjectId("5397eb925d2664177b0fc5aa"),
                    "answer" : "Item Question 2 - Answer 1",
                    "isCorrectAnswer" : false
                },
                {
                    "_id" : ObjectId("5397eb925d2664177b0fc5ab"),
                    "answer" : "Item Question 2 - Answer 2",
                    "isCorrectAnswer" : true
                }
            ]
        }
    ]
}
Sign up to request clarification or add additional context in comments.

3 Comments

its removed those questions array
No pal its working for me. Check the updated answer.
Check this in Mongoplayground: mongoplayground.net/p/cpf0oSX_29l

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.