Docs Menu

Docs HomeView & Analyze DataMongoDB Compass

Specify Read Preference and Tags

On this page

  • Procedure
  • Command Line Example
  • Configuration File Example
  • Learn More

You can use the forceConnectionOptions option to set required connection option values on Compass. By setting required connection options such as read preference, you can restrict how users connect to your MongoDB deployment and ensure that their read operations don't impact the availability of replica set primaries.

In this example, you limit read operations to read only from SECONDARY replica set members with the ANALYTICS and READ_ONLY tags. You can specify these options on the command line or in a configuration file.

The following command starts MongoDB Compass from the command line and uses --forceConnectionOptions to set the readPreference and readPreferenceTags options:

<path-to-Compass-executable> \
--forceConnectionOptions.readPreference=secondary \
--forceConnectionOptions.readPreferenceTags=nodeType:ANALYTICS \
--forceConnectionOptions.readPreferenceTags=nodeType:READ_ONLY

Note

The name and filepath of the Compass executable depend on your operating system.

You can specify the Compass configuration file in either EJSON or YAML format. To set the readPreference and readPreferenceTags, use --forceConnectionOptions:

{
"forceConnectionOptions": [
["readPreference", "secondary"],
["readPreferenceTags", "nodeType:ANALYTICS"],
["readPreferenceTags", "nodeType:READ_ONLY"]
]
}
forceConnectionOptions:
- readPreference: secondary
- readPreferenceTags: nodeType:ANALYTICS
- readPreferenceTags: nodeType:READ_ONLY

To learn more about the MongoDB Compass configuration file, see Configuration File Settings.

← Restrict Write Operations to MongoDB