If you get the “$pullAll requires an array argument but was given a double” error in MongoDB, it’s because you didn’t provide an array as the value to replace.
Example
Suppose we have a collection with the following documents:
{ "_id" : 1, "bar" : [ 1, 7, 2, 3, 8, 7, 1 ] } { "_id" : 2, "bar" : [ 0, 1, 8, 17, 18, 8 ] } { "_id" : 3, "bar" : [ 15, 11, 8, 0, 1, 3 ] }
And we want to use $pullAll
to replace all occurrences of a value in one of those documents.
Problem Code
Here’s an example of code that causes the above error:
db.foo.update(
{ _id: 1 },
{ $pullAll: { bar: 7 } }
)
Result:
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 2, "errmsg" : "$pullAll requires an array argument but was given a double" } })
This happened because we forgot to surround the value 7
with square brackets. In other words, we forgot to provide an array.
Solution
Here’s the same code except this time we’ve provided the value as an array:
db.foo.update(
{ _id: 1 },
{ $pullAll: { bar: [ 7 ] } }
)
Result:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
This shows us that one document was matched and modified.
Let’s take a look at the collection now:
db.foo.find()
Result:
{ "_id" : 1, "bar" : [ 1, 2, 3, 8, 1 ] } { "_id" : 2, "bar" : [ 0, 1, 8, 17, 18, 8 ] } { "_id" : 3, "bar" : [ 15, 11, 8, 0, 1, 3 ] }
The document has been updated successfully.