31

I want to remove this:

{
"val" : NumberLong(200),
"chId" : 2,
"reqSys" : "222220005031",
"old" : NumberLong(223),
"isRb" : false
},

from this:

{
"_id" : ObjectId("52d7c25480f0a83293adbbbc"),
"d" : 2014001,
"m" : 123456789,
"topups" : {
    "data" : [
            {
                    "val" : NumberLong(200),
                    "chId" : 2,
                    "reqSys" : "222220005031",
                    "old" : NumberLong(223),
                    "isRb" : false
            },
            {
                    "val" : NumberLong(150),
                    "chId" : 2,
                    "reqSys" : "222220005031",
                    "old" : NumberLong(166),
                    "isRb" : false
            }
    ],
    "total" : {
            "cnt" : 2,
            "revenue" : NumberLong(3500000)
    }
}

I would like to find the object by querying {d:2014001, m:123456789} and remove the whole object in the data array that has "val":200 if that is possible in one command. But if not, multiple commands work for me also. Have tried with $pull and $pullAll but I am missing something.

1

1 Answer 1

52

To remove sub document use $pull

This query will remove from nested sub document

 db.collection.update({ d : 2014001 , m :123456789},
                      {$pull : { "topups.data" : {"val":NumberLong(200)} } } )
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.