I have a simple document with field products which is an array of products.
{
"products" : [
{
"amount" : 0,
"id" : "9647562"
}
],
}
I want to remove all objects in array with amount 0. I thought this is pretty easy:
db.shop.update({}, {"$pull": {"products": {"amount": 0} }});
This does not work but this does:
db.shop.update({}, {"$pull": {"products": {"amount": 2} }});
So basically If I change number from 2 to 0 it doesn’t work anymore. As you can see amount is integer and I’m also trying to remove it as integer, so this is not the case. What else can I try?
try this :
Excerpt from
updatedocumentation:So,here you should have upsert = false , and multi = true.
Reason : Your matching criteria is
{}which means match all, hence the update statement exits after matching the first row, and not doing anything to it, to change all matching rows, you need to provide the flagmultias true.Example: