Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-109

When using MongoClient.connect, native_parser defaults to true.

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.0
    • Affects Version/s: 1.3, 1.4
    • Component/s: None
    • Labels:
    • Environment:
      Windows 8 x64, MongoDB 2.4.6, node-mongodb-native version 1.3.23 (though many other previous versions appear to contain this bug)

      I was tracking down a bug in the BSON parser (which I will file separately after this), and I discovered that depending on how I used MongoClient, the native_parser option had different defaults. The documentation states in many places that the native_parser defaults to false.

      Specifically, on http://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html it says that the "db" option of MongoClient.connect takes "a hash off options to set on the db object, see Db constructor"

      The Db constructor page says "native_parser {Boolean, default:false},"

      Simple way to reproduce:

      var MongoClient = require('mongodb').MongoClient;
      var Server = require('mongodb').Server;
      
      var client = new MongoClient(new Server("localhost", 27017), {});
      client.connect("mongodb://localhost:27017/test", function(err, db) {
          console.log(db.native_parser); // Prints true.
          db.close();
      });
      

      But if I use client.open, instead of client.connect:

      var MongoClient = require('mongodb').MongoClient;
      var Server = require('mongodb').Server;
      
      var client = new MongoClient(new Server("localhost", 27017), {});
      client.open(function(err, client) {
          var db = client.db("test");
          console.log(db.native_parser); // Prints undefined, which is falsy.
          db.close();
      });
      

      The line causing the issue seems to be in lib\mongodb\mongo_client.js:

      // If we have no db setting for the native parser try to set the c++ one first
      object.db_options.native_parser = _setNativeParser(object.db_options);
      

      _setNativeParser looks like this:

      var _setNativeParser = function(db_options) {
        if(typeof db_options.native_parser == 'boolean') return db_options.native_parser;
      
        try {
          require('bson').BSONNative.BSON;
          return true;
        } catch(err) {
          return false;
        }
      }
      

      As you can see, if you don't specify any value for native_parser and then call 'connect', it will default to the native parser, which is contrary to what to docs seem to say should happen, and to what other methods do.

      I suggest removing the call to and definition of _setNativeParser, unless there is some design constraint I am unaware of. At least making the docs more clear about what will happen would be helpful, if this is the intended functionality.

      Under normal node-mongodb-native usage this likely wouldn't be an issue, but when I was trying to track down a bug in the BSON parser, it was really annoying to have it switch between the native parser and the JS parser for seemingly no reason (when really it had to do with how the database connection is set up.) It would be good to have consistency.

      I look forward to your response, and can prepare a patch or a pull request should this be necessary.

            Assignee:
            christkv Christian Amor Kvalheim
            Reporter:
            tylerchurch Tyler Church
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: