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?
db.collection.updateMany({}, {"$pull": { "array": undefined }});?