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

    • Bug
    • Status: Closed
    • Critical - P2
    • Resolution: Fixed
    • 3.6.3, 4.0.0
    • 4.0.0, 3.6.4
    • Shell
    • 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

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

            Dates

              Created:
              Updated:
              Resolved: