Docs HomeMongoDB Compass

Specify Read Preference and Tags 指定读取首选项和标记

You can use the forceConnectionOptions option to set required connection option values on Compass. 可以使用forceConnectionOptions选项在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.通过设置所需的连接选项,如读取首选项,您可以限制用户连接到MongoDB部署的方式,并确保他们的读取操作不会影响副本集主副本的可用性。

Procedure过程

In this example, you limit read operations to read only from SECONDARY replica set members with the ANALYTICS and READ_ONLY tags. 在本例中,您将读取操作限制为仅从具有ANALYTICSREAD_ONLY标记的SECONDARY副本集成员读取。You can specify these options on the command line or in a configuration file.可以在命令行或配置文件中指定这些选项。

Command Line Example命令行示例

The following command starts MongoDB Compass from the command line and uses --forceConnectionOptions to set the readPreference and readPreferenceTags options:以下命令从命令行启动MongoDB Compass,并使用--forceConnectionOptions设置readPreferencereadPreferenceTags选项:

<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.Compass可执行文件的名称和文件路径取决于您的操作系统。

Configuration File Example配置文件示例

You can specify the Compass configuration file in either EJSON or YAML format. 您可以以EJSON或YAML格式指定Compass配置文件。To set the readPreference and readPreferenceTags, use --forceConnectionOptions:要设置readPreferencereadPreferenceTags,请使用--forceConnectionOptions

EJSON

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

YAML

forceConnectionOptions:
- readPreference: secondary
- readPreferenceTags: nodeType:ANALYTICS
- readPreferenceTags: nodeType:READ_ONLY

Learn More了解更多信息

To learn more about the MongoDB Compass configuration file, see Configuration File Settings.要了解有关MongoDB Compass配置文件的更多信息,请参阅配置文件设置。