Reputation: 44632
Check out the following query:
db.models.findOne({ "value.userId": { $ne: null } }, { "value.userId":1}).value.userId == null
This says:
Since we're returning only records without null values, comparing the result of that to null should always return false. However, this always returns true.
If I just do the .findOne(...) and print that instead of doing the comparison, I get:
{ "_id": 4, "value": { "userId": null }
Anyone have a clue what's going on here?
EDIT: the "type" of this field is apparently 6 - not 10, which is what a null should be. EDIT2: apparently type 6 is "undefined" - not sure why it prints null...
Upvotes: 2
Views: 710
Reputation: 213203
Well the issue here is when you try to print an undefined
value, MongoDB prints null
rather than issuing you an error.
See this example: -
> db.foo.save({"amount": undefined})
> db.foo.find({"amount": {$ne: null}})
{ "_id" : ObjectId("50d0d3a1511dd1035f01c636"), "amount" : null }
So, as you are saying, the type of userId
is 6
, which is for undefined
, so it prints null
. Also, it is $ne: null
, so it is matched too.
See this link: - http://realprogrammer.wordpress.com/2012/11/04/null-undefined-nan-and-missing-property-goto-considered-harmful-part-2/
However, if you see the same behaviour with null
, you will get the desired result: -
> db.foo.save({"amount": null})
> db.foo.findOne({"amount": {$ne: null}})
null
Upvotes: 3