0

I can do a $pull for a simple document:

> db.users.find();
{ "_id" : 4, "sessions" : [ { "sid" : "foo1" }, { "sid" : "bar" } ] }

> db.users.update({}, {"$pull": {sessions: {"sid" : "bar"}}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

But if I have more information on the session:

> db.users.insert({_id:5, sessions: [ { sid: "foo"}, {sid:"bar", ssid:"test"}]});
> db.users.find();
{ "_id" : 4, "sessions" : [ { "sid" : "foo1" } ] }
{ "_id" : 5, "sessions" : [ { "sid" : "foo" }, { "sid" : "bar", "ssid" : "test" } ] }

> db.users.update({}, {"$pull": {sessions: {"sid" : "bar"}}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

then the $pull fails.

1
  • Umm. Your query matches both documents and you forgot the "multi" param. The update would work as expected if it matched the right document, or affected both. Commented Nov 10, 2014 at 3:49

1 Answer 1

1

Your $pull is correct, but you need to add the multi: true option for the update to apply to both docs:

db.users.update({}, {"$pull": {sessions: {"sid" : "bar"}}}, {multi: true});
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.