使用API配置设置
配置config
API提供方法来检查和更新mongosh
配置。使用config
API进行的更新将在会话之间持久存在。
打印当前的mongosh
配置
config
返回<property>
的当前值
config.get( "<property>" )
将<property>
的当前设置更改为<value>
config.set( "<property>", <value> )
将<property>
重置为默认值
config.reset( "<property>" )
支持的property
参数
键 | 类型 | 默认值 | 描述 |
---|---|---|---|
displayBatchSize | 整数 | 20 | 每个游标迭代显示的项目数 |
enableTelemetry | 布尔值 | true | 启用向MongoDB发送匿名跟踪和诊断数据。 |
editor | 字符串 | null | 指定在 mongosh 控制台中使用的编辑器。如果设置,将覆盖EDITOR 环境变量。 |
forceDisableTelemetry | 布尔值 | false | 仅在全局配置文件中可用。当为true时,用户无法手动启用遥测。 |
historyLength | 整数 | 1000 | 要存储在 mongosh REPL的历史文件中的项目数。 |
inspectCompact | 整数或布尔值 | 3 | |
inspectDepth | 整数或Infinity | 6 | 打印对象的深度。将 inspectDepth 设置为Infinity (javascript对象)将打印所有嵌套对象到其完整深度。 |
redactHistory | 字符串 | remove | 控制记录在shell历史中的信息。必须是以下之一
|
showStackTraces | 布尔值 | false | 控制错误消息和堆栈跟踪的显示。 |
snippetAutoload | 布尔值 | true | 如果设置为 true ,则在启动时自动加载已安装的片段。 |
snippetIndexSourceURLs | 字符串 | 由分号分隔的URL列表,链接到一个片段注册表。 | |
snippetRegistryURL | 字符串 |
行为
从历史记录中移除或红字处理敏感信息
mongosh
会尽力匹配通常对应于某些类型敏感信息的模式。
以下是一些匹配的模式
证书和密钥
电子邮件地址
通用用户目录
HTTP(s) URL
IP 地址
MongoDB 连接字符串
某些操作,如connect()
,被认为是固有的敏感操作。如果redactHistory
设置为remove
或remove-redact
,则包含这些操作的行将从命令行历史记录中删除。
其他操作,如find()
,有时包含敏感信息,如电子邮件地址。除非redactHistory
设置为remove-redact
,否则shell历史记录将保留这些行。
使用配置文件的配置行为
使用 config
API 指定的设置
覆盖配置文件中指定的设置配置文件。
持久化跨重启。
示例
考虑以下配置文件,它将 inspectDepth
设置为 20
mongosh: inspectDepth: 20
在您的 mongosh
会话期间,您运行以下命令将 inspectDepth
设置为 10
config.set( "inspectDepth", 10 )
inspectDepth
的值变为 10
,即使 mongosh
重启,它也将保持 10
。
示例
更新游标返回的项目数量
考虑查看包含大量文档的集合。您可以更新 batchSize
以限制游标返回的项目数量。
config.set("displayBatchSize", 3)
未来的 db.collection.find()
操作将每次迭代仅返回 3 个文档。
开启堆栈跟踪
启用堆栈跟踪以查看更详细的错误报告。
config.set("showStackTraces", true)
输出如下所示
// 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)
从外部 mongosh
调用 config
API
您可以使用 mongosh
的 --eval
选项从命令行调用 config
API。在这种情况下,--nodb
选项意味着 mongosh
将在不连接到 MongoDB 数据库的情况下更新。
重要
您必须在 --eval
表达式和 config
属性之间使用不同的引号。也就是说,一个用单引号,另一个用双引号。
mongosh --nodb --eval 'config.set("enableTelemetry", true)'
mongosh
在 API 调用的结果中返回附加信息。
Current Mongosh Log ID: 609583b730e14918fa0d363f Using MongoDB: undefined Using Mongosh Beta: 0.12.1 For mongosh info see: https://mongodb.ac.cn/docs/mongodb-shell/ Setting "enableTelemetry" has been changed
删除敏感信息
当 redactHistory
设置为 remove-redact
或 remove
时,比较召回历史。
将 redactHistory
设置为 remove-redact
模式,并输入包含电子邮件地址的查询。
config.set( "redactHistory", "remove-redact" ) db.contacts.find( {"email": "customer@clients.com" } )
按下 上箭头
重新播放最后一个命令时,电子邮件地址被删除。
db.contacts.find( {"email": "<email>" } ) // Redacted
将 redactHistory
设置为 remove
模式,并输入包含电子邮件地址的查询。
config.set( "redactHistory", "remove" ) db.contacts.find( {"email": "customer@clients.com" } )
按下 上箭头
重新播放最后一个命令时,电子邮件地址仍然存在。
db.contacts.find( {"email": "customer@clients.com" } )
Shell 历史记录反映了更改。(注意,它首先存储最近的输入。)
db.contacts.find( {"email": "customer@clients.com" } ) config.set( "redactHistory", "remove" ) db.contacts.find( {"email": "<email>" } ) config.set( "redactHistory", "remove-redact" )
将配置设置重置为默认值
如果您修改了配置设置并希望将其重置为默认值,请使用 config.reset("<属性>")
。
将
historyLength
设置的值更改为2000
config.set("historyLength", 2000) 验证
historyLength
的更新值config.get("historyLength") 将
historyLength
设置重置为默认值1000
config.reset("historyLength") 验证
historyLength
的更新值config.get("historyLength")