Reputation: 113335
I have objects like in my collection:
{
"foo": {
"actions": [
...,
"delete"
]
},
"bar": {
"tags": ["links", ...]
}
}
I want to remove delete
value from foo.actions
from all objects that contain links
value in bar.tags
field.
I tried to do a basic update that seems not to work:
db.foo.update({
"bar.tags": /links/
}, {
$pull: {
"foo.actions": "delete"
}
}, {
multi: true
});
delete
field is not removed since the following request doesn't return 0
:
> db.foo.find({"bar.tags": /links/, "foo.actions": /delete/}).count()
786
What's the issue here? Why delete
value is not removed?
Upvotes: 1
Views: 903
Reputation: 9178
It worked for me. Maybe you're not calling the right collection. What you want to do is print the count before and after the update to see if there's a difference.
Example:
db.foo.find({"bar.tags": /links/, "foo.actions": /delete/}).count() // 4
db.foo.update({
"bar.tags": /links/
}, {
$pull: {
"foo.actions": "delete"
}
}, {
multi: true
});
db.foo.find({"bar.tags": /links/, "foo.actions": /delete/}).count() // 0
Setup
var createObj = function(){
var o = {
"foo": {
"actions": [
"ok"
]
},
"bar": {
"tags": ["links"]
}
}
if( Math.random() < 0.5 ){
o.foo.actions.push("delete");
o.foo.actions.push("delete");
o.foo.actions.push("delete");
}
return o;
};
use test;
db.foo.drop();
for(var i = 0; i < 10; i++){
db.foo.insert( createObj() );
}
Upvotes: 2