[SERVER-39222] mongo shell should parse readPreference and readPreferenceTags from connection string Created: 28/Jan/19  Updated: 27/Oct/23  Resolved: 31/Jan/23

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Marko Vojvodic Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Gone away Votes: 8
Labels: move-stm
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-48769 Shell does not respect read preferenc... Closed
Assigned Teams:
Server Tooling & Methods
Participants:
Case:

 Description   

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.



 Comments   
Comment by Chris Harris [ 31/Jan/23 ]

This ticket was opened against the legacy mongo shell which has since been deprecated and removed. The newer MongoDB Shell (mongosh) accepts and respects all configurations in a connection string.

Comment by Steven Vannelli [ 10/May/22 ]

Moving this ticket to the Backlog and removing the "Backlog" fixVersion as per our latest policy for using fixVersions.

Generated at Thu Feb 08 04:51:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.