Confused as to how $pullAll works in MongoDB

424 views
Skip to first unread message

Alex

unread,
May 4, 2011, 7:54:25 PM5/4/11
to mongodb-user
Hello,

I've got a document that looks like this:

db.blog.findOne()
{
"_id" : ObjectId("4dc1c938c4bfb4d21a000001"),
"blogid" : 1,
"body" : "Lorem ipsum dolor",
"comments" : [
{
"id" : 1,
"name" : "Alex",
"comment" : "Test",
"approved" : 1
},
{
"id" : 2,
"name" : "Phil",
"comment" : "Test",
"approved" : 1
},
{
"id" : 3,
"name" : "Joe",
"comment" : "Test",
"approved" : 0
}
],
"no_comments" : 11,
"title" : "Hello world"
}

If I run the query

db.blog.update({'blogid':1}, { $pull : { 'comments' : {'approved' :
0} } });

Then it will remove the third comment.

If instead I want to pull all comments where approved is 0 or 1 the
following query doesn't work:

db.blog.update({'blogid':1}, { $pullAll : { 'comments' : {'approved' :
[0,1]} } });

I get the error

Modifier $pushAll/pullAll allowed for arrays only

Can someone please explain where I'm going wrong?

Thank you

Nat

unread,
May 4, 2011, 8:06:39 PM5/4/11
to mongodb-user
$pullAll only supports value (not criteria) array. So it doesn't
support the use case you have.
http://www.mongodb.org/display/DOCS/Updating#Updating-%24pullAll

But you can use $in with $pull instead
db.blog.update({'blogid':1}, { $pull : { 'comments' : {'approved':
{$in : [0,1]}} } });

Alex

unread,
May 4, 2011, 8:10:34 PM5/4/11
to mongodb-user
Thank you but I don't quite understand, could you show me an example
of a pullAll query that uses a value array?

Thanks

On May 5, 1:06 am, Nat <nat.lu...@gmail.com> wrote:
> $pullAll only supports value (not criteria) array. So it doesn't
> support the use case you have.http://www.mongodb.org/display/DOCS/Updating#Updating-%24pullAll

Scott Hernandez

unread,
May 4, 2011, 8:13:09 PM5/4/11
to mongod...@googlegroups.com
{ _id:1, colors: [ "red", "blue", "yellow" ] }

update({}, { $pullAll: { colors : ["red", "blue"] } })

--
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.


Alex

unread,
May 4, 2011, 8:16:09 PM5/4/11
to mongodb-user
Brilliant. Thank you

Nat

unread,
May 4, 2011, 8:27:17 PM5/4/11
to mongod...@googlegroups.com
In your case, you want to use $pull though as $pullAll supports only exact match.
Reply all
Reply to author
Forward
0 new messages