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

Unable to configure readPreference for map reduce

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.2.25
    • Affects Version/s: None
    • Component/s: None

      Hello everybody, according to the documentation it should be possible to set the readPreference as another key in the options object:

      'use strict';
      
      const { MongoClient, ReadPreference } = require('mongodb');
      const assert = require('assert');
      
      MongoClient.connect('mongodb://localhost:37017/testing')
          .then(db => {
              const map = function() {
                  emit(this._id, this._id);
              };
      
              const reduce = function(key, values) {
                  return 123;
              };
      
              return db.collection('items').mapReduce(map, reduce, {
                  out: {
                      inline: 1
                  },
                  readPreference: ReadPreference.SECONDARY_PREFERRED
              })
          })
          .then(console.log)
          .catch(console.error);
      

      However I'm getting the following error when I try it:

      { MongoError: unknown m/r field for sharding: readPreference
          at Function.MongoError.create (/private/tmp/tp-20170214102859/node_modules/mongodb-core/lib/error.js:31:11)
          at /private/tmp/tp-20170214102859/node_modules/mongodb-core/lib/connection/pool.js:483:72
          at authenticateStragglers (/private/tmp/tp-20170214102859/node_modules/mongodb-core/lib/connection/pool.js:429:16)
          at Connection.messageHandler (/private/tmp/tp-20170214102859/node_modules/mongodb-core/lib/connection/pool.js:463:5)
          at Socket.<anonymous> (/private/tmp/tp-20170214102859/node_modules/mongodb-core/lib/connection/connection.js:319:22)
          at emitOne (events.js:96:13)
          at Socket.emit (events.js:188:7)
          at readableAddChunk (_stream_readable.js:176:18)
          at Socket.Readable.push (_stream_readable.js:134:10)
          at TCP.onread (net.js:551:20)
        name: 'MongoError',
        message: 'unknown m/r field for sharding: readPreference',
        ok: 0,
        errmsg: 'unknown m/r field for sharding: readPreference' }
      

      The instance listening at localhost:37017 is a mongos running 3.2.12, the collection is sharded on "_id" (hashed) and the driver is 2.2.24.

      While looking for a solution I noticed that in some code examples the readPreference option is instead passed embedded within the "out" option. If I do that, I won't get any errors and the query seems to work. However, I'm not sure if that option is not simply ignored there. How can I verify? Thank you for looking into this.

            Assignee:
            christkv Christian Amor Kvalheim
            Reporter:
            jiripospisil Jiri Pospisil
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: