Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-39222

mongo shell should parse readPreference and readPreferenceTags from connection string

    • Type: Icon: Improvement Improvement
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Shell
    • Server Tooling & Methods

      When I try to connect using read preference and read preference tags via the Mongo shell v4.0.0, it doesn't behave as I would expect given current documentation. For example, the docs say:

      The primary read preference mode is not compatible with read preference modes that use tag sets or maxStalenessSeconds. If you specify tag sets or a maxStalenessSeconds value with primary, the driver will produce an error.

      When the read preference includes tag sets, the client attempts to find secondary members that match the specified tag sets and directs reads to a random secondary from among the nearest group of matching secondaries. If no secondaries have matching tags, the read operation produces an error.

      However, when I try to connect and execute the following command.js script:

      db.getSiblingDB("test").foo.find()
      

      I get the following behavior (removed some replication logs for brevity):

      The script:

      mongo "mongodb+srv://db.example.com/test?readPreferenceTags=location:US" --username username --password password < command.js
      mongo "mongodb+srv://db.example.com/test?readPreference=secondary&readPreferenceTags=location:US" --username username --password password < command.js
      mongo "mongodb+srv://db.example.com/test?readPreference=secondary&readPreferenceTags=location:CA" --username username --password password < command.js
      mongo "mongodb+srv://db.example.com/test?readPreference=secondary&readPreferenceTags=location:RS" --username username --password password < command.js
      

      The output (minus unnecessary log lines):

      MongoDB shell version v4.0.0
      connecting to: mongodb+srv://db.example.com/test?readPreferenceTags=location:US
      MongoDB server version: 4.0.0
      { "_id" : ObjectId("5c3fcdc67ffe2f5b036bdd98"), "a" : 1 }
      bye
      MongoDB shell version v4.0.0
      connecting to: mongodb+srv://db.example.com/test?readPreference=secondary&readPreferenceTags=location:US
      MongoDB server version: 4.0.0
      { "_id" : ObjectId("5c3fcdc67ffe2f5b036bdd98"), "a" : 1 }
      bye
      MongoDB shell version v4.0.0
      connecting to: mongodb+srv://db.example.com/test?readPreference=secondary&readPreferenceTags=location:CA
      MongoDB server version: 4.0.0
      { "_id" : ObjectId("5c3fcdc67ffe2f5b036bdd98"), "a" : 1 }
      bye
      MongoDB shell version v4.0.0
      connecting to: mongodb+srv://db.example.com/test?readPreference=secondary&readPreferenceTags=location:RS
      MongoDB server version: 4.0.0
      { "_id" : ObjectId("5c3fcdc67ffe2f5b036bdd98"), "a" : 1 }
      bye
      

      The same output occurs for the non-SRV connection string, e.g.

      mongo "mongodb://db-0.example.com:27017,db-1.example.com:27017,db-2.example.com:27017/test?replicaSet=db-0&readPreferenceTags=location:US" --ssl --authenticationDatabase admin --username username --password password < command.js
      mongo "mongodb://db-0.example.com:27017,db-1.example.com:27017,db-2.example.com:27017/test?replicaSet=db-0&readPreference=secondary&readPreferenceTags=location:US" --ssl --authenticationDatabase admin --username username --password password < command.js
      mongo "mongodb://db-0.example.com:27017,db-1.example.com:27017,db-2.example.com:27017/test?replicaSet=db-0&readPreference=secondary&readPreferenceTags=location:CA" --ssl --authenticationDatabase admin --username username --password password < command.js
      mongo "mongodb://db-0.example.com:27017,db-1.example.com:27017,db-2.example.com:27017/test?replicaSet=db-0&readPreference=secondary&readPreferenceTags=location:RS" --ssl --authenticationDatabase admin --username username --password password < command.js
      

      When I connect manually using one of those connection strings, and I run db.getMongo().getReadPrefMode() and db.getMongo().getReadPrefTagSet(), both returned nothing, leading me to believe the shell is not properly parsing the connection string parameters.

      Given that we have functions inside of the shell to manually specify the read preference, it would be nice if the mongo shell could instead parse the readPreference and readPreferenceTags mongo URI options if specified.

            Assignee:
            backlog-server-stm Backlog - Server Tooling and Methods (STM) (Inactive)
            Reporter:
            marko.vojvodic@mongodb.com Marko Vojvodic
            Votes:
            8 Vote for this issue
            Watchers:
            19 Start watching this issue

              Created:
              Updated:
              Resolved: