Docs HomeMongoDB Shell

Configure Settings Using a Configuration File使用配置文件配置设置

You can specify mongosh settings in a global configuration file. 您可以在全局配置文件中指定mongosh设置。When you specify settings in a configuration file, those settings are applied at startup. 在配置文件中指定设置时,这些设置将在启动时应用。After you create a configuration file, the settings in the file take effect the next time you start mongosh.创建配置文件后,文件中的设置将在下次启动mongosh时生效。

Configuration File Format配置文件格式

The mongosh configuration file uses the YAML format. mongosh配置文件使用YAML格式。All options are under the mongosh namespace.所有选项都在mongosh命名空间下。

Example Configuration File配置文件示例

The following configuration file sets:以下配置文件集:

  • displayBatchSize to 50为50
  • inspectDepth to 20
  • redactHistory to remove-redact
mongosh:
displayBatchSize: 50
inspectDepth: 20
redactHistory: "remove-redact"

Configuration File Location配置文件位置

The file location where mongosh looks for the configuration file depends on your operating system:mongosh查找配置文件的文件位置取决于您的操作系统:

Operating System操作系统File Location文件位置
Windowsmongosh.cfg, in the same directory as the mongosh.exe binary.mongosh.cfg,位于与mongosh.exe二进制文件相同的目录中。
macOSmongosh looks for a configuration file in the following directories in the order they are listed:按照列出的顺序在以下目录中查找配置文件:
  1. /usr/local/etc/mongosh.conf
  2. /opt/homebrew/etc/mongosh.conf
  3. /etc/mongosh.conf
Once mongosh reads a configuration file in one of these directories, any remaining directories in the list are not checked and configuration files in those directories are ignored.一旦mongosh读取了其中一个目录中的配置文件,列表中的任何剩余目录都不会被检查,这些目录中的设置文件也会被忽略。
Linux/etc/mongosh.conf

Configurable Settings可配置设置

You can specify the following mongosh settings in your configuration file:您可以在配置文件中指定以下mongosh设置:

KeyType类型Default违约Description描述
displayBatchSizeinteger20The number of items displayed per cursor iteration每次游标迭代显示的项目数
enableTelemetrybooleantrueEnables sending anonymized tracking and diagnostic data to MongoDB.允许向MongoDB发送匿名跟踪和诊断数据。
editorstringnullDesignates an editor to use within the mongosh console. 指定要在mongosh控制台中使用的编辑器。Overrides the EDITOR environment variable if set.覆盖EDITOR环境变量(如果已设置)。
forceDisableTelemetrybooleanfalseOnly available in the global configuration file. 仅在全局配置文件中可用。When true, users cannot enable telemetry manually.如果为true,则用户无法手动启用遥测。
historyLengthinteger1000The number of items to store in mongosh REPL's history file.要存储在mongosh REPL的历史文件中的项目数。
inspectCompactinteger or boolean3

The level of inner elements that mongosh outputs on a single line. mongosh在一行中输出的内部元素的级别。Short array elements are also grouped together on a single line.短数组元素也被分组在一条线上。
If set to false, mongosh outputs each field on its own line.如果设置为falsemongosh将在自己的行上输出每个字段。

inspectDepthinteger or Infinity6The depth to which objects are printed. Setting inspectDepth to Infinity (the javascript object) prints all nested objects to their full depth.打印对象的深度。将inspectDepth设置为Infinity(javascript对象)会将所有嵌套对象打印到其全部深度。
redactHistorystringremoveControls what information is recorded in the shell history. Must be one of:控制shell历史记录中记录的信息。必须是以下其中之一:
  • keep: Retain all history.:保留所有历史记录。
  • remove: Remove lines which contain sensitive information.:删除包含敏感信息的行。
  • remove-redact: Redact sensitive information.:重新处理敏感信息。
showStackTracesbooleanfalseControls display of a stack trace along with error messages.控制堆栈跟踪以及错误消息的显示。
snippetAutoloadbooleantrueIf true, automatically load installed snippets at startup.如果为true,则在启动时自动加载已安装的snippets
snippetIndexSourceURLsstringMongoDB RepositoryA semicolon-separated list of URLs that link to a snippet registry.链接到snippet注册表的URL的分号分隔列表。
snippetRegistryURLstringnpm RegistryThe npm registry used by the mongosh npm client that installs snippet.安装snippetmongosh npm客户端使用的npm注册表。

Behavior with config APIconfigAPI的行为

Settings specified with the config API:使用配置API指定的设置:

  • Override settings specified in the configuration file.覆盖配置文件中指定的设置。
  • Persist across restarts.在重新启动时坚持。
Example

Consider the following configuration file that sets the inspectDepth setting to 20:考虑以下配置文件,该文件将inspectDepth设置设置为20

mongosh:
inspectDepth: 20

During your mongosh session you run the following command to set inspectDepth to 10:mongosh会话期间,您可以运行以下命令将inspectDepth设置为10

config.set( "inspectDepth", 10 )

The value of inspectDepth becomes 10, and will remain 10 even when mongosh is restarted.inspectDepth的值变为10,即使在mongosh重新启动时也将保持为10。