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支持的属性参数
browser | string or boolean |
| |
disableLogging | boolean | false | |
disableSchemaSampling | boolean | false |
|
displayBatchSize | integer | 20 | |
enableTelemetry | boolean | true | |
editor | string | null | mongosh console. Overrides the EDITOR environment variable if set.mongosh控制台中使用的编辑器。如果已设置,则覆盖EDITOR环境变量。 |
forceDisableTelemetry | boolean | false | |
historyLength | integer | 1000 | mongosh REPL's history file.mongosh REPL历史文件中的项目数。 |
inspectCompact | integer or boolean | 3 |
|
inspectDepth | integer or Infinity | 6 | inspectDepth to Infinity (the javascript object) prints all nested objects to their full depth.inspectDepth设置为Infinity(javascript对象)会将所有嵌套对象打印到其完整深度。 |
logCompressionEnabled | boolean | false | true, MongoDB Shell uses gzip to compress logs. true时,MongoDB Shell使用gzip压缩日志。 |
logLocation | string | 请参阅查看Shell日志。 | |
logMaxFileCount | integer or Infinity | 100 | |
logRetentionDays | integer or Infinity | 30 | Infinity, log files are not deleted based on age. Infinity,则不会根据年龄删除日志文件。 |
logRetentionGB | float or Infinity | Unset |
|
oidcRedirectUri | string | http://localhost:27097/redirect | http://localhost:27097/redirect.http://localhost:27097/redirect。 |
oidcTrustedEndpoints | array of strings | [] (empty array) | localhost. Access tokens are sent to these endpoints. Only configure endpoints that you trust.localhost。访问令牌被发送到这些端点。仅配置您信任的端点。 |
redactHistory | string | remove |
|
showStackTraces | boolean | false | |
snippetAutoload | boolean | true | true, automatically load installed snippets at startup.true,则在启动时自动加载已安装的代码段。 |
snippetIndexSourceURLs | string | MongoDB Repository | |
snippetRegistryURL | string | npm Registry | 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设置为remove或remove-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指定的设置:
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。
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 mongosh从mongosh外部调用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
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 changedRedact Sensitive Information更正敏感信息
Compare the recalled history when 当redactHistory is set to remove-redact or remove.redactHistory设置为remove-redact或remove时,比较召回的历史记录。
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>" )。
Change the value of the将historyLengthsetting to2000:historyLength设置的值更改为2000:config.set("historyLength", 2000)Verify the updated value for验证historyLength:historyLength的更新值:config.get("historyLength")Reset the将historyLengthsetting to the default value of1000:historyLength设置重置为默认值1000:config.reset("historyLength")Verify the updated value for验证historyLength:historyLength的更新值:config.get("historyLength")