$pull is slightly special in that the document passed in (excluding
the fieldname) is actually a selector that is applied to each item in
the array.
Scope the docs:
http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull
Here's an example that -- I believe -- addresses your concern (note
that $elemMatch is not needed):
> db.test.find()
{ "_id" : ObjectId("..."), "a" : [ { "x" : 1, "y" : 2 }, { "x" : 2,
"y" : 1 } ], "user" : "adam" }
{ "_id" : ObjectId("..."), "a" : [
{
"x" : 1,
"y" : 2
},
{
"x" : 3,
"y" : 1
},
{
"x" : 4,
"y" : 3
}
], "user" : "bob" }
{ "_id" : ObjectId("..."), "a" : [ { "x" : 4, "y" : 3 } ], "user" :
"chet" }
> db.test.update({ "user": "bob" }, { "$pull": { "a": { "x": 4, "y": 3 } } })
> db.test.find()
{ "_id" : ObjectId("..."), "a" : [ { "x" : 1, "y" : 2 }, { "x" : 2,
"y" : 1 } ], "user" : "adam" }
{ "_id" : ObjectId("..."), "a" : [ { "x" : 1, "y" : 2 }, { "x" : 3,
"y" : 1 } ], "user" : "bob" }
{ "_id" : ObjectId("..."), "a" : [ { "x" : 4, "y" : 3 } ], "user" :
"chet" }
Thanks,
- Brandon
On Oct 18, 3:38 pm, Jake <
askmat...@gmail.com> wrote:
> user_doc = {
> "username" : "foouser",
> "emails" : [
> {
> "email" : "
foous...@example.com",
> "primary" : True
> },
> {
> "email" : "
foous...@example.com",
> "primary" : False
> },
> {
> "email" : "
foous...@example3.com",
> "primary" : False
> }
> ]
> }
>
> ### Method One
>
> collection.update({"username" : "foouser"},
> {"$pull" : {"emails" : {"email" :
> "
foous...@example.com"}}})
>
> ### Method Two
>
> collection.update({"username" : "foouser"},
> {"$pull" : {"emails" :
> {"$elemMatch" : {"email" :
> "
foous...@example3.com",
> "primary" : False}}}})
>
> Method one works as expected. It can remove the
>
> {
> "email" : "
foous...@example.com",
> "primary" : True
> }
>
> from emails array.
>
> In method two, I try to remove
>
> {
> "email" : "
foous...@example3.com",