I have objects in my collection that look like:
{ MyArray:[null, \"some value\", null] }
I want to query those objects that have a null value a
In order to find documents with arrays, with null elements please run:
db.collection.find({"keyWithArray":{$elemMatch:{"$in":[null], "$exists":true}}})
According to safaribooksonline proper null
matching is performed using $in
(because you cannot use $eq
with null
).
Also, comparing with null
:
{"something":null}
will match documents with "something" field set to null and every document which does not have "something" field at all. Thus we have to make sure the key exists using $exists
.