$pull vs. $pullAll

1,373 views
Skip to first unread message

Daniel W

unread,
Jan 5, 2012, 10:43:30 AM1/5/12
to mongodb-user
I'm using pymongo, and I'm confused why this had the desired effect:

db.POV3.update({'_id':foo['_id']}, {'$pull': {'vhst': {u's': u'NY',
u'vtd': u'', u'd': datetime.datetime(2002, 1, 1, 0, 0), u't':
u'PRIM'}}})

...but this did not: ($pullAll instead of $pull, with the value to be
pulled put into an array)

db.POV3.update({'_id':foo['_id']}, {'$pullAll': {'vhst': [{u's':
u'NY', u'vtd': u'', u'd': datetime.datetime(2002, 1, 1, 0, 0), u't':
u'PRIM'}]}})

The relevant part of the document in mongo was:

"vhst" : [
{
"d" : ISODate("2002-01-01T00:00:00Z"),
"s" : "NY",
"t" : "GEN",
"vtd" : "X"
},
{
"d" : ISODate("2002-01-01T00:00:00Z"),
"s" : "NY",
"t" : "PRIM",
"vtd" : ""
},
{
"s" : "NY",
"vtd" : "X",
"d" : ISODate("2002-01-01T00:00:00Z"),
"t" : "PRIM"
}
],


thanks!

Tyler Brock

unread,
Jan 5, 2012, 11:41:16 AM1/5/12
to mongodb-user
You put brackets around the value for key "vhst" in the first example
and not in the second.

-Tyler

Daniel W

unread,
Jan 5, 2012, 12:12:12 PM1/5/12
to mongodb-user
I think you mean brackets on the second example, not the first....

and I did that because:

{ $pull : { field : _value } }

vs.

{ $pullAll : { field : value_array } }

for pullAll, I'm providing an array of values, hence the []

Tyler Brock

unread,
Jan 5, 2012, 2:02:19 PM1/5/12
to mongodb-user
Order within the document matters, try this one:

db.POV3.update({
'_id': foo['_id']
},
{
'$pullAll': {
'vhst': [{
'd': new Date(Date.UTC(2002, 0, 1)),
's': 'NY',
't': 'PRIM',
'vtd': '',
}]
}
})

I was able to see this query remove my test document (which looks like
yours) on my system.

-Tyler

Daniel W

unread,
Jan 5, 2012, 2:18:39 PM1/5/12
to mongodb-user
so order within the document matters for $pullAll but not for $pull?
Is there a reason for that?

Scott Hernandez

unread,
Jan 5, 2012, 3:03:22 PM1/5/12
to mongod...@googlegroups.com
Yes, $pull creates a query spec to match array elements, and $pullAll
takes a list of actual array elements (where order matters) to return,
not a query.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>

Tyler Brock

unread,
Jan 5, 2012, 3:11:20 PM1/5/12
to mongodb-user
Nice Scott,

I thought this stack overflow description was rather good as well:
http://stackoverflow.com/questions/8598685/mongodb-pull-all-element-from-the-array

-Tyler

Daniel W

unread,
Jan 5, 2012, 3:14:29 PM1/5/12
to mongodb-user
Ah OK. Makes sense. Thanks guys

On Jan 5, 3:11 pm, Tyler Brock <ty...@10gen.com> wrote:
> Nice Scott,
>
> I thought this stack overflow description was rather good as well:http://stackoverflow.com/questions/8598685/mongodb-pull-all-element-f...
Reply all
Reply to author
Forward
0 new messages