Check out the following query:
db.models.findOne({ "value.userId": { $ne: null } }, { "value.userId":1}).value.userId == null
This says:
- Find a record that has a non-null user id field
- Return the user id in the result
- Compare that value to null
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…
Well the issue here is when you try to print an
undefinedvalue, MongoDB printsnullrather than issuing you an error.See this example: –
So, as you are saying, the type of
userIdis6, which is forundefined, so it printsnull. 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: –