Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused as to how $pullAll works in MongoDB

Tags:

mongodb

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

like image 439
alexbilbie Avatar asked May 04 '11 23:05

alexbilbie


People also ask

How to remove all element from array in MongoDB?

The $pullAll operator: The $pullAll operator removes all the elements from the defined array. Now, use the following query to remove all values of 8 from the array where _id is 1. We can verify this by checking the collection using the find() method. We have successfully removed element 8 from the array in document 1.

What is $pull in Mongodb?

The $pull operator removes from an existing array all instances of a value or values that match a specified condition. The $pull operator has the form: { $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } } To specify a <field> in an embedded document or in an array, use dot notation.


1 Answers

db.blog.update({"blogid":1},{$pullAll:{"comments":{"name":"Phil","comment":{$eq:"Test"}}}})

WriteResult({
        "nMatched" : 0,
        "nUpserted" : 0,
        "nModified" : 0,
        "writeError" : {
                "code" : 2,
                "errmsg" : "$pullAll requires an array argument but was given a object"
        }
})

db.blog.update({"blogid":1},{$pull:{"comments":{"name":"Phil","comment":{$eq:"Test"}}}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

$pull update operator:

  • It works if the element is either array or array with Object.
  • It works even if you use any condition operator in query syntax.

$pullAll update operator:

  • It only works if the element is array.
  • It only works if you don't use any condition operator in query syntax
like image 97
Soura Ghosh Avatar answered Oct 27 '22 10:10

Soura Ghosh