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

MongoClient.readPreference returns "primary" ignoring readPref from connection string

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical - P2
    • Resolution: Fixed
    • Affects Version/s: 3.6.3, 4.0.0
    • Fix Version/s: 4.0.0, 3.6.4
    • Component/s: Shell
    • Labels:
      None

      Description

      Problem Description

      It seems the driver is not handling readPreference as expected when specified in the connection string.

      This affects both the current 3.6.3 release as well as the current 4.0.0 beta version.

      After some input by Matt it turns out this is not a duplicate of NODE-2807. The fix in NODE-2807 fixed it when being passed directly as an additional parameter when creating the MongoClient. However, when using only the connection string, the readPreference is not parsed.

      In short this does not work:

      const client = await MongoClient.connect(
          'mongodb://root:password123@localhost:28001/?directConnection=true&readPreference=secondary'
      );
      

      This will work:

      const client = await MongoClient.connect(
          'mongodb://root:password123@localhost:28001/?directConnection=true', { readPreference: 'secondary' }
      );
      

       

      Steps to Reproduce

      1. Create a new MongoClient instance by connecting to node specifying readPreference=secondary in the connection string, e.g.:
        mongodb://root:password@localhost:28001/?readPreference=secondary&directConnection=true
      2. Print the value of client.readPreference.mode.

      Expected Results

      The value is secondary.

      Actual Results

      The value is primary.

       

        Attachments

          Activity

            People

            Assignee:
            eric.adum Eric Adum (Inactive)
            Reporter:
            michael.rose Michael Rose (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: