Lesson 2: Configuring the MongoDB Shell / Learn

Code Summary: Configuring the MongoDB Shell

The following sections explain how to configure the MongoDB Shell, or mongosh, by using the config API, the configuration file, and the --eval flag.

Configure mongosh by Using the config API

The config API allows you to configure the behavior of mongosh by retrieving, setting, and resetting configuration options. To get a list of the available configuration options, run the following command:

config

The available configuration options are as follows:

- `displayBatchSize` - The number of documents to display when using the `it` iterator.
- `maxTimeMS` - The maximum amount of time to allow a query to run.
- `enableTelemetry` - Whether to enable telemetry.
- `editor` - The editor to use when editing code.
- `snippetIndexSourceURLs` - The URLs to use when fetching snippet index files.
- `snippetRegistryURL` - The URL to use when fetching snippet registry files.
- `snippetAutoload` - Whether to automatically load snippets.
- `inspectCompact` - Whether to use compact mode when inspecting objects.
- `inspectDepth` - The maximum depth to use when inspecting objects.
- `historyLength` - The number of history entries to keep.
- `showStackTraces` - Whether to show stack traces when errors occur.
- `redactHistory` - Whether to redact sensitive information from history.

To retrieve the value of a specific configuration option, use the config.get() method. As an example, here’s the command to get the current value of enableTelemetry:

config.get('enableTelemetry') // returns true or false

To set the value of a specific configuration option, use the config.set() method, and pass in the name of the option and the value that you want to set:

config.set('enableTelemetry', false)

To reset the value of a specific configuration option, use the config.reset() method, and pass in the name of the option that you want to reset:

config.reset('enableTelemetry')

Configure mongosh by Using the Configuration File

You can also configure mongosh by using a configuration file called mongosh.conf. The location of this file depends on your operating system. The following code snippet shows the location of the file on Windows, macOS, and Linux systems:

- Windows: `mongosh.cfg`, in the same directory as the `mongosh.exe` binary.
- macOS:
 - `/usr/local/etc/mongosh.conf`
 - `/etc/mongosh.conf`
 - `/opt/homebrew/etc/mongosh.conf`
- Linux: `/etc/mongosh.conf`

The mongosh.conf file uses YAML format. The following code shows how to configure the displayBatchSize, inspectDepth, and redactHistory options in mongosh.conf:

mongosh: 
 displayBatchSize: 50 
 inspectDepth: 20 
 redactHistory: "remove-redact"

Configure mongosh by Using the --eval Flag

Finally, you can set configuration options in mongosh by using the --eval flag with a command. For example, to disable telemetry, you would run the following command to invoke the disableTelemetry() function:

mongosh --eval "disableTelemetry()"

You can also use the --eval flag to run queries and other commands. For example, to run a query for some documents, you would run the following command:

mongosh --eval "db.accounts.find().limit(3)" --quiet