Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-10586

Mongo shell's "null" vs. "undefined" madness continues

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.4.5
    • Component/s: Shell
    • None
    • Environment:
      Mac OS X 10.8.4 / XCode 4.6.3 / Homebrew updated daily
      CentOS 6.4
    • ALL
    • Hide

      For some empty collection "foo", try the following:

      > var bar;
      > db.foo.save(

      {bar: bar}

      );

      Now, look at the document that was created:

      > db.foo.findOne();

      { "_id" : ObjectId("5213ede21269f7d12659b3ea"), "bar" : null }

      Oh, okay, so I can isolate such a document to remove it from a dataset, right? WRONG.

      > db.foo.find(

      {bar: null}

      ).length()
      0
      > db.foo.find({bar: {$type: 10}}).length()
      0
      > db.foo.find({bar: {$type: 6}}).length()
      1

      Oh, so it was `undefined` all along? That makes sense, except that it lied to me and said it was `null`. So that means the following single line will cause the same problems, won't it?

      > db.foo.save(

      {bar: undefined}

      )

      Yes. Yes it will.

      Show
      For some empty collection "foo", try the following: > var bar; > db.foo.save( {bar: bar} ); Now, look at the document that was created: > db.foo.findOne(); { "_id" : ObjectId("5213ede21269f7d12659b3ea"), "bar" : null } Oh, okay, so I can isolate such a document to remove it from a dataset, right? WRONG. > db.foo.find( {bar: null} ).length() 0 > db.foo.find({bar: {$type: 10}}).length() 0 > db.foo.find({bar: {$type: 6}}).length() 1 Oh, so it was `undefined` all along? That makes sense, except that it lied to me and said it was `null`. So that means the following single line will cause the same problems, won't it? > db.foo.save( {bar: undefined} ) Yes. Yes it will.

      Using the Mongo shell, I am able to create documents whose properties should be `undefined`; these properties are printed to the screen as `null`, however, and therefore they were difficult to remove. The code that I have included below is deceptively simple, but it is distilled from production code. It indicates that there are fundamental problems in the way that MongoDB developers treat JavaScript – either they do not understand it themselves, or they do not expect others to understand it. It's obvious that Mongo's shell breaks with standards-compliant behavior in collection preallocation, for example, but this error is unacceptable. MongoDB's "untyped" nature is very useful for cleaning up messy datasets, as opposed to SQL, but in this case it actually made things much more difficult because I knew there were `null` values in there somewhere, but I couldn't actually find them.

            Assignee:
            Unassigned Unassigned
            Reporter:
            wilkinson Sean Wilkinson
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: