2

Check out the following query:

 db.models.findOne({ "value.userId": { $ne: null } }, { "value.userId":1}).value.userId == null

This says:

  1. Find a record that has a non-null user id field
  2. Return the user id in the result
  3. 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...

2
  • Strange this is. Because, it is giving expected result for me. Are you sure this is working this way? Commented Dec 18, 2012 at 20:01
  • Positive...just ran this: db.models.findOne({ "value.userId": { $ne: null } }).value.userId, and it returns null. Commented Dec 18, 2012 at 20:03

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

11 Comments

Well now that's just annoying.
@jvenema. Well, that may seem annoying now, but actually this feature is very useful, when you accidentally try to print an undefined field, in which case you won't get errors. That is how MongoDB is designed to be.
I get that it's designed to "be that way", but the behaviour should be consistent - either print and filter nulls and undefined values the same way (both), or don't - but don't print one way, and filter another...
@jvenema.. Actually a field is undefined either by giving it explicit value of undefined, or when it is not there at all in the document. So, of course you cannot filter on the basis of a field that is not there, i.e., a non-existence or undefined field cannot be equal to null.
Sorta - in the find() call you can't filter on null == undefined, but in the console they can be compared (see my original question - comparing to null is true), and they obviously are considered to be equal "enough" for the purposes of writing to the output - otherwise you would see "undefined" and not "null" as the output of the script. The problem is inconsistency - in some cases "null" cannot be compared and is not the same as "undefined", but in others it is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.