Reputation: 172
I have added undefined values to array using script performed on a server side (Mongo version - 3.4.10).
Let's say I have document like
{
"array" : ["a","b", undefined]
}
and I want to remove undefined element.
When I query it like this
find({"array" : {$elemMatch : {$in : [null], $exists : true} }})
It returns above document - Mongo treats null and undefined as equal.
However, below update query is not removing element as expected (works fine for nulls)
update({} ,{ $pull : {"array" : {$in : [null]} }}, {multi : true})
It is also not possible to use undefined as math expression
InMatchExpression equality cannot be undefined
Question: How do you remove elements in such a situation?
Upvotes: 0
Views: 2153
Reputation: 3459
db.collection.update(
{},
{$pull: {"address.coord":{$type: 6}}},
{multi:true}
)
Refer the type & pull operators for more details.
It will remove all the undefined
matches as per your requirement. If your document structure have nested arrays, use $[]
positional operators.
Upvotes: 4