Docs Home / mongosh / Configure / Configure Settings

Configure Settings Using the API使用API配置设置

The config API provides methods to examine and update the mongosh configuration. Updates made using the config API persist between sessions.configAPI提供了检查和更新mongosh配置的方法。使用configAPI进行的更新在会话之间持续。

Note

The config API can be used within the mongosh command line interface. It has no effect in the embedded Compass shell.configAPI可以在mongosh命令行界面中使用。它在嵌入式Compass shell中没有效果。

Syntax语法

Print the current mongosh configuration:打印当前mongosh配置:

config

Return the current value for <property>:返回<property>的当前值:

config.get( "<property>" )

Change the current setting of <property> to <value>:<property>的当前设置更改为<value>

config.set( "<property>", <value> )

Reset a <property> to the default value:<property>重置为默认值:

config.reset( "<property>" )

Supported property parameters支持的属性参数

KeyType类型Default默认Description描述
browserstring or booleanSystem default browser系统默认浏览器

Specifies the browser mongosh redirects you to when MONGODB-OIDC is enabled.指定启用MONGODB-OIDC时浏览器mongosh将您重定向到的位置。

This option is run with the system shell.此选项与系统shell一起运行。

Set browser to false to disable opening browsers entirely.browser设置为false以完全禁用打开浏览器。

disableLoggingbooleanfalseSpecifies whether MongoDB Shell writes log entries.指定MongoDB Shell是否写入日志条目。
disableSchemaSamplingbooleanfalse

By default, MongoDB Shell samples your database schema to enable autocomplete suggestions.默认情况下,MongoDB Shell会对数据库模式进行采样,以启用自动完成建议。

If you set this setting to true, MongoDB Shell does not sample your database schema and autocomplete behaves as if the collection is empty.如果将此设置设置为true,MongoDB Shell不会对数据库模式进行采样,自动补全的行为就像集合为空一样。

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. Overrides the EDITOR environment variable if set.指定要在mongosh控制台中使用的编辑器。如果已设置,则覆盖EDITOR环境变量。
forceDisableTelemetrybooleanfalseOnly available in the global configuration file. When true, users cannot enable telemetry manually.仅在全局配置文件中可用。当为真时,用户无法手动启用遥测。
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. Short array elements are also grouped together on a single line.mongosh在单行上输出的内部元素的级别。短数组元素也被组合在一条线上。

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对象)会将所有嵌套对象打印到其完整深度。
logCompressionEnabledbooleanfalseSpecifies whether MongoDB Shell compresses log files. When this value is true, MongoDB Shell uses gzip to compress logs. 指定MongoDB Shell是否压缩日志文件。当此值为true时,MongoDB Shell使用gzip压缩日志。See Enable Log Compression.请参阅启用日志压缩
logLocationstringDepends on your operating system. See View Shell Logs.这取决于你的操作系统。
请参阅查看Shell日志
Directory where MongoDB Shell writes log files. Specify an absolute filepath. See Specify Log File Location.MongoDB Shell写入日志文件的目录。指定绝对文件路径。请参见指定日志文件位置
logMaxFileCountinteger or Infinity100Maximum number of log files that the MongoDB Shell retains. After the MongoDB Shell reaches the maximum log file count, it starts deleting the oldest log files until the count is below the threshold. MongoDB Shell保留的最大日志文件数。MongoDB Shell达到最大日志文件计数后,它开始删除最旧的日志文件,直到计数低于阈值。See Modify Maximum Log File Count.请参见修改最大日志文件计数
logRetentionDaysinteger or Infinity30Number of days that MongoDB Shell log files are retained. If this value is set to Infinity, log files are not deleted based on age. MongoDB Shell日志文件保留的天数。如果此值设置为Infinity,则不会根据年龄删除日志文件。To prevent log storage from growing too large, always specify at least one log retention criterion. See Log Retention.为防止日志存储增长得太大,请始终至少指定一个日志保留标准。请参阅日志保留
logRetentionGBfloat or InfinityUnset

Maximum amount of space in gigabytes used for MongoDB Shell logs. If the combined size of MongoDB Shell logs exceeds this value, log files are deleted starting with the oldest logs.MongoDB Shell日志使用的最大空间量(GB)。如果MongoDB Shell日志的组合大小超过此值,则从最旧的日志开始删除日志文件。

If this value is set to Infinity, log files are not deleted based on storage size. To prevent log storage from growing too large, always specify at least one log retention criterion. 如果此值设置为Infinity,则不会根据存储大小删除日志文件。为防止日志存储增长得太大,请始终至少指定一个日志保留标准。See Log Retention.请参阅日志保留

oidcRedirectUristringhttp://localhost:27097/redirectSpecifies a URI where the identity provider redirects you after authentication. The URI must match the configuration of the identity provider. The default is http://localhost:27097/redirect.指定身份验证后身份提供程序重定向URI。URI必须与身份提供程序的配置匹配。默认值为http://localhost:27097/redirect
oidcTrustedEndpointsarray of strings[] (empty array)Specifies an array of trusted endpoint hostnames, including possible wildcards, that are not Atlas or localhost. Access tokens are sent to these endpoints. Only configure endpoints that you trust.指定一组受信任的端点主机名,包括可能的通配符,这些通配符不是Atlas或localhost。访问令牌被发送到这些端点。仅配置您信任的端点。
redactHistorystringremove

Controls what information is recorded in the mongosh command history. You can access the mongosh command history via the up and down keys or Ctrl+R while inside the shell.控制mongosh命令历史记录中记录的信息。在shell中,您可以通过上下键或Ctrl+R访问mongosh命令历史记录。

Must be one of the following values:必须是以下值之一:

  • keep: Retain all history.:保留所有历史。
  • remove: Remove lines that contain sensitive commands from the history. For example, remove removes calls to db.auth() or connect().:从历史记录中删除包含敏感命令的行。例如,remove会删除对db.auth()connect()的调用。
  • remove-redact: Remove lines that contain sensitive commands and redact specific strings in other history lines, such as file paths, email addresses, and URLs.:删除包含敏感命令的行,并在其他历史行中编辑特定字符串,如文件路径、电子邮件地址和URL。
showStackTracesbooleanfalseControls display of a stack trace along with error messages.控制堆栈跟踪和错误消息的显示。
snippetAutoloadbooleantrueIf true, automatically load installed snippets at startup.如果为true,则在启动时自动加载已安装的代码段
snippetIndexSourceURLsstringMongoDB RepositoryA semicolon-separated list of URLs that link to a snippet registry.链接到代码段注册表的以分号分隔的URL列表。
snippetRegistryURLstringnpm RegistryThe npm registry used by the mongosh npm client that installs snippet.安装代码段mongosh npm客户端使用的npm注册表。

Behavior行为

Remove or Redact Sensitive Information From History从历史记录中删除或更正敏感信息

mongosh makes "best-effort" attempts to match patterns that normally correspond to certain kinds of sensitive information.尽“最大努力”尝试匹配通常与某些类型的敏感信息相对应的模式。

There are patterns that match:有一些模式是匹配的:

  • Certificates and keys证书和键
  • Email addresses电子邮件地址
  • Generic user directories通用用户目录
  • HTTP(s) URLs
  • IP addresses网际协议地址
  • MongoDB connection stringsMongoDB连接字符串

Certain operations, such as connect(), are considered inherently sensitive. If redactHistory is set to remove or remove-redact, lines with these operations will be removed from the command line history.某些操作,如connect(),被认为具有固有的敏感性。如果redactHistory设置为removeremove-redact,则具有这些操作的行将从命令行历史记录中删除。

Other operations, like find(), sometimes have sensitive information like email addresses. 其他操作,如find(),有时会包含电子邮件地址等敏感信息。The shell history will retain these lines as entered unless redactHistory is set to remove-redact.除非redactHistory设置为删除redact,否则shell历史记录将保留这些输入的行。

Behavior with Configuration File配置文件的行为

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

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。

Examples示例

Update Number of Items Returned by a Cursor更新游标返回的项目数

Consider viewing a collection with a number of large documents. You can update the batchSize to limit the number of items returned by a cursor.考虑查看包含许多大型文档的集合。您可以更新batchSize以限制游标返回的项目数。

config.set("displayBatchSize", 3)

Future db.collection.find() operations will only return 3 documents per cursor iteration.未来的db.collection.find()操作每次游标迭代只会返回3个文档。

Turn On Stack Traces打开堆栈跟踪

Enable stack traces to see more detailed error reporting.启用堆栈跟踪以查看更详细的错误报告。

config.set("showStackTraces", true)

The output differs like this:输出如下所示:

// showStackTraces set to 'false'
Enterprise> db.orders.find( {}, { $thisWontWork: 1 } )
MongoError: FieldPath field names may not start with '$'.

// showStackTraces set to 'true'
Enterprise> db.orders.find( {}, { $thisWontWork: 1 } )
Uncaught:
MongoError: FieldPath field names may not start with '$'.
at MessageStream.messageHandler (/usr/bin/mongosh:58878:20)
at MessageStream.emit (events.js:315:20)
at MessageStream.EventEmitter.emit (domain.js:548:15)
at processIncomingData (/usr/bin/mongosh:57954:12)
at MessageStream._write (/usr/bin/mongosh:57850:5)
at writeOrBuffer (_stream_writable.js:352:12)
at MessageStream.Writable.write (_stream_writable.js:303:10)
at Socket.ondata (_stream_readable.js:719:22)
at Socket.emit (events.js:315:20)
at Socket.EventEmitter.emit (domain.js:548:15)

Call config API from outside mongoshmongosh外部调用config API

You can call the config API from the command line using --eval with mongosh. 您可以使用带有mongosh--eval从命令行调用配置API。In this case the --nodb option means mongosh will update without connecting to a MongoDB database.在这种情况下,--nodb选项意味着mongosh将在不连接到MongoDB数据库的情况下进行更新。

Important

You must use different quotation marks for the --eval expression and the config property. That is, single quotes for one and double quotes for the other.您必须为--eval表达式和config属性使用不同的引号。也就是说,一个用单引号,另一个用双引号。

mongosh --nodb --eval 'config.set("enableTelemetry", true)'

mongosh returns additional information along with the result of the API call.返回附加信息以及API调用的结果。

Current Mongosh Log ID:      609583b730e14918fa0d363f
Using MongoDB: undefined
Using Mongosh Beta: 0.12.1

For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/

Setting "enableTelemetry" has been changed

Redact Sensitive Information更正敏感信息

Compare the recalled history when redactHistory is set to remove-redact or remove.redactHistory设置为remove-redactremove时,比较召回的历史记录。

Set redactHistory to remove-redact mode and enter a query containing an email address.redactHistory设置为remove-redact模式,并输入包含电子邮件地址的查询。

config.set( "redactHistory", "remove-redact" )
db.contacts.find( {"email": "customer@clients.com" } )

When you press the up arrow to replay the last command the email address is redacted.当您按向上箭头重放最后一个命令时,电子邮件地址将被编辑。

db.contacts.find( {"email": "<email>" } )  // Redacted

Set redactHistory to remove mode and enter a query containing an email address.redactHistory设置为remove模式,并输入包含电子邮件地址的查询。

config.set( "redactHistory", "remove" )
db.contacts.find( {"email": "customer@clients.com" } )

When you press the up arrow to replay the last command the email address is present.当您按向上箭头重放最后一个命令时,电子邮件地址就会出现。

db.contacts.find( {"email": "customer@clients.com" } )

The shell history reflects the changes. (Note that this stores the most recent input first.)shell历史反映了这些变化。(请注意,这将首先存储最近的输入。)

db.contacts.find( {"email": "customer@clients.com" } )
config.set( "redactHistory", "remove" )
db.contacts.find( {"email": "<email>" } )
config.set( "redactHistory", "remove-redact" )

Reset a Configuration Setting to the Default Value将配置设置重置为默认值

If you modified a configuration setting and want to reset it to the default value, use config.reset( "<property>" ).如果您修改了配置设置并希望将其重置为默认值,请使用config.reset( "<property>" )

  1. Change the value of the historyLength setting to 2000:historyLength设置的值更改为2000

    config.set("historyLength", 2000)
  2. Verify the updated value for historyLength:验证historyLength的更新值:

    config.get("historyLength")
  3. Reset the historyLength setting to the default value of 1000:historyLength设置重置为默认值1000

    config.reset("historyLength")
  4. Verify the updated value for historyLength:验证historyLength的更新值:

    config.get("historyLength")