does elemMatch work with pull in pymongo

886 views
Skip to first unread message

Jake

unread,
Oct 18, 2011, 3:38:43 PM10/18/11
to mongodb-user
user_doc = {
"username" : "foouser",
"emails" : [
{
"email" : "foou...@example.com",
"primary" : True
},
{
"email" : "foou...@example.com",
"primary" : False
},
{
"email" : "foou...@example3.com",
"primary" : False
}
]
}

### Method One

collection.update({"username" : "foouser"},
{"$pull" : {"emails" : {"email" :
"foou...@example.com"}}})

### Method Two

collection.update({"username" : "foouser"},
{"$pull" : {"emails" :
{"$elemMatch" : {"email" :
"foou...@example3.com",
"primary" : False}}}})

Method one works as expected. It can remove the

{
"email" : "foou...@example.com",
"primary" : True
}

from emails array.

In method two, I try to remove

{
"email" : "foou...@example3.com",
"primary" : False
}

with the help of **$elemMatch** but it doesn't work for me.

Any suggestion is appreciated.
Thank you

Brandon Diamond

unread,
Oct 18, 2011, 4:19:24 PM10/18/11
to mongodb-user
$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",

Jake

unread,
Oct 18, 2011, 4:59:03 PM10/18/11
to mongodb-user
Hello Brandon,

It works for me know

thank you:)

Brandon Diamond

unread,
Oct 18, 2011, 5:23:01 PM10/18/11
to mongodb-user
My pleasure! :)
Reply all
Reply to author
Forward
0 new messages