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

MongoClient.connect readPreference option not working

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.2.30
    • Affects Version/s: 2.2.29
    • Component/s: None
    • Labels:
    • Environment:
      Replica Set - PRIMARY + 2 x SECONDARY
      Node - 4.8.3

      The new MongoClient.connect readPreference option not working ok when is a ReadPreference class.
      http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html

      In the following example I have a replica set with 3 members:

      {
              mongo1: {type: 'PRIMARY', tag: 'america'},
              mongo2: {type: 'SECONDARY', tag: 'europe'},
              mongo3: {type: 'SECONDARY', tag: 'africa'}
      }

      First test is a connection with readPreference string nearest and readPreferenceTags - continent: europe

      Second test is a connection with readPreference class with nearest and tags - continent: europe

      /*
       cfg = rs.conf();
       cfg.members[0].tags.continent = 'america'; // primary    - host = mongo1
       cfg.members[1].tags.continent = 'europe';  // secondary  - host = mongo2
       cfg.members[2].tags.continent = 'africa';  // secondary  - host = mongo3
       rs.reconfig(cfg);
       */
      
      const mongodb = require('mongodb'),
          async = require('async'),
          MongoClient = mongodb.MongoClient,
          ReadPreference = mongodb.ReadPreference;
      
      
      const
          MAP = {
              mongo1: {type: 'PRIMARY', tag: 'america'},
              mongo2: {type: 'SECONDARY', tag: 'europe'},
              mongo3: {type: 'SECONDARY', tag: 'africa'}
          },
          CONNECT_URL = 'mongodb://localhost:27017/folder',
          CONNECT_OPTIONS_LEGACY = {
              replicaSet: 'rs',
              readPreference: 'secondary',
              readPreferenceTags: [
                  {
                      "continent": "europe"
                  }
              ]
          },
          CONNECT_OPTIONS_NEW = {
              replicaSet: 'rs',
              readPreference: new ReadPreference('secondary', [{
                  "continent": "europe"
              }])
          };
      
      
      function initConnection(options, cb) {
          MongoClient.connect(CONNECT_URL, options, function (err, db) {
              cb(err, db);
          })
      }
      
      function find(db, cb) {
          var filesCollection = db.collection('files');
          async.times(10, function (index, next) {
              filesCollection.find().explain(function (err, result) {
                  console.log(index + ' - ' + result.serverInfo.host + ' - ' + MAP[result.serverInfo.host].type + ' ' + MAP[result.serverInfo.host].tag);
                  next();
              });
          }, function () {
              cb();
          });
      }
      
      console.log('Start test!')
      async.waterfall([
              function testLegacyOptions(cb) {
                  console.log();
                  console.log('Test options with readPreferenceTags!')
                  console.log('Read preference: secondary!')
                  console.log('Read preference tags: continent-europe!')
                  initConnection(CONNECT_OPTIONS_LEGACY, cb);
              },
              find,
              function testNewOptions(cb) {
                  console.log();
                  console.log('Test options with replicaSet instance!')
                  console.log('Read preference: secondary!')
                  console.log('Read preference tags: continent-europe!')
                  initConnection(CONNECT_OPTIONS_NEW, cb);
              },
              find
          ],
          function (err) {
              console.log();
              console.log('Testing done!')
          })
      

      And the output is

      Start test!
      
      Test options with readPreferenceTags!
      Read preference: secondary!
      Read preference tags: continent-europe!
      the options [readPreferenceTags] is not supported
      1 - mongo2 - SECONDARY europe
      0 - mongo2 - SECONDARY europe
      4 - mongo2 - SECONDARY europe
      2 - mongo2 - SECONDARY europe
      3 - mongo2 - SECONDARY europe
      8 - mongo2 - SECONDARY europe
      7 - mongo2 - SECONDARY europe
      5 - mongo2 - SECONDARY europe
      6 - mongo2 - SECONDARY europe
      9 - mongo2 - SECONDARY europe
      
      Test options with replicaSet instance!
      Read preference: secondary!
      Read preference tags: continent-europe!
      0 - mongo1 - PRIMARY america
      1 - mongo1 - PRIMARY america
      2 - mongo1 - PRIMARY america
      4 - mongo1 - PRIMARY america
      3 - mongo1 - PRIMARY america
      5 - mongo1 - PRIMARY america
      7 - mongo1 - PRIMARY america
      8 - mongo1 - PRIMARY america
      9 - mongo1 - PRIMARY america
      6 - mongo1 - PRIMARY america
      
      Testing done!
      

            Assignee:
            matt.broadstone@mongodb.com Matt Broadstone
            Reporter:
            mciocan Marius Ciocan
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: