0
{
"_id" : ObjectId("5cca927ed5494b0"),
"userName": "1234",
"rcReviews": [{
        "userName": "qwert",
        "finalReview": "qtrwyw",
        "dField": [{
                "name": "t2",
                "status": "Not Verified",
                "reviewComment": "asdfag"
            },
            {
                "status": "Not Verified",
                "reviewComment": "asegqrt",
                "name": "t13"
            },
            {
                "name": "c452",
                "status": "Not Verified",
                "reviewComment": "gaeqrtqa "
            },
            {
                "status": "Not Verified",
                "reviewComment": "tyqiqooqa",
                "name": "c13"
            }
        ]
    },
    {
        "userName": "1517",
        "finalReview": "wsywx",
        "dField": [{
                "status": "Not Verified",
                "reviewComment": "aswrrwgqrt",
                "name": "t113"
            },
            {
                "name": "c1516",
                "status": "Not Verified",
                "reviewComment": "gaeqredectqa "
            },
        ]
    }
]}

I want a query which checks dField array key exists in rcReveiws array. If it exists then it has remove the dField array and other contents should be there only dField array should be removed

2
  • Hi! What have you tried? Commented May 20, 2020 at 9:29
  • db.newcopy.updateMany({ "rcReviews": { "$elemMatch": { "$elemMatch": { "dField": { "$exists": true } } } } }, { "$unset": { "rcReviews.$[].$[].dField": "" }} ) I had tried like this. Thank you for responding Commented May 20, 2020 at 10:20

1 Answer 1

2

You can use the below query. It has been tested with the data provided by you

db.testmongo.update({"rcReviews.dField":{$exists:true}},{$unset:{"rcReviews.$[].dField":""}},{multi:true})

Change the collection name to match yours. The query checks for records that contain the dField attribute in rcReviews array. It updates all the objects inside that array and removes the dField attribute using unset

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

1 Comment

Thank you Abhishek Kothari. I have another doubt, if i wanted to remove another key inside the array and that key is type object so if i change name that's enough right no need to change whole query??

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.