1

I have a document that has a nested array. I want to delete an item in the array by it's key value. I've tried doing an update with:

array('$unset' => array('item.key' => 1))

array('$pull' => array('item.key' => 1))

Doing, $unset , kept the key but nulled out the value, I want to get rid of it entirely. Pull didn't do anything.

Any suggestions?

Thank you!

4

1 回答 1

2

In order to use $pull you have to specify the entire array element you want to delete, luckily $pull is special in that you can specify a match criteria for the element you are $pulling

Here is an example:

db.array.update({},{$pull:{a:{"key2":{$exists:true}}}},{multi:true})

This will delete array element where "key2" exists as a keyname regardless of the value. Sounds like this is what you want.

Here is a reference.

于 2012-11-04T21:19:40.650 回答