Configure the MongoDB CLI
On this page
You can define the settings that the MongoDB CLI uses to interact with
MongoDB services like Atlas, Cloud Manager, and Ops Manager by using
the mongocli config command. The first time you run this
command, the MongoDB CLI creates a configuration file called mongocli.toml
to store your settings.
Prerequisites
To configure the MongoDB CLI:
Add your IP or CIDR block to the API access list for your MongoDB service.
Generate a Programmatic API Key if you don't already have one.
Note
Programmatic API Keys are role-based. Different roles have different privileges and capabilities, and some MongoDB CLI commands are available to some roles but not others. Be sure that the roles assigned to your Programmatic API Key are appropriate for the commands you intend to use.
To learn more about generating a Programmatic API Key for your MongoDB service, see the following documentation:
Procedure
The mongocli config command varies slightly depending on the MongoDB service for which you want to configure the MongoDB CLI.
Create a profile.
This command creates a profile called
default
in the MongoDB CLI Configuration File.
Run the mongocli config command:
mongocli config
This command creates a profile with a
custom name in the MongoDB CLI Configuration File. To use this profile
in a command, you must specify the profile with the
--profile
flag.
Run the mongocli config command:
mongocli config --profile <profile-name>
Optional: Enter your Programmatic API Key.
When prompted, enter the Public API Key
and Private API Key
for your Atlas deployment.
? Public API Key: [? for help] abcdef ? Private API Key: [? for help] ************************************
Choose your default organization.
If you entered API keys, the command displays the name and ID of organizations that your API key can access.
Select your organization:
Press the
Down Arrow
andUp Arrow
keys to highlight the desired organization, orType the organization name to filter the available organizations.
? Default Org ID: [Use arrows to move, type to filter] Org1 (5e39bf1212121e685774c81c) Press
Enter
.
If you didn't enter API keys, the command prompts you to enter your
Default Organization ID
.
Choose your default project.
If you entered API keys, the command displays the name and ID of projects that your API key can access.
Select your project:
Press the
Down Arrow
andUp Arrow
keys to highlight the desired project, orType the project name to filter the available projects.
? Default Project ID: [Use arrows to move, type to filter] Project1 (5e5ebffd0c04a97009061234) Project2 (5cfacee6014b761b07f15678) Project3 (5e39bf4979358e6857741212) Project4 (5c815cc7014b768fb67e3434) Press
Enter
.
If you didn't enter API keys, the command prompts you to enter your
Default Project ID
.
Choose your default output format.
Press the
Down Arrow
andUp Arrow
keys to highlight the desired default output format:? Default Output Format: [Use arrows to move, type to filter] plaintext json OptionDescriptionplaintext
Human-readable output that includes all fields that MongoDB CLI returns.json
JSON output that includes all fields that MongoDB CLI returns.Press
Enter
.
Specify the path to the MongoDB Shell, mongosh
, on your system.
MongoDB CLI uses mongosh
path that you specify to allow you to
access your deployments. The default value is
/usr/local/bin/mongosh
. Press Enter
to accept the default
or:
Specify the path to
mongosh
on your system.? Default MongoDB Shell Path: [? for help] (/usr/local/bin/mongosh) Press
Enter
.
Verify your profile.
View the configuration file or run the mongocli config describe command to verify your profile.
Important
Your API keys are like passwords. The configuration file stores API keys in plaintext, so ensure that you secure the configuration file appropriately.
Example
The following command returns the settings in the default profile:
mongocli config describe default
The preceding command returns the following settings. For security purposes, the command redacts API keys in the output.
SETTING VALUE mongosh_path /usr/local/bin/mongosh org_id 5f1f39ffc902201990f12345 private_api_key redacted project_id 7ehf04abc10defb66c7d1234c public_api_key redacted service cloud
Example
The following command returns the settings
in the profile named myMongoDB
:
mongocli config describe myMongoDB
The command returns following settings. For security purposes, the command redacts API keys in the output.
SETTING VALUE org_id 5f1f39ffc902201990f12345 private_api_key redacted project_id 7ehf04abc10defb66c7d1234c public_api_key redacted service cloud
Create a profile.
This command creates a profile called
default
in the MongoDB CLI Configuration File.
Run the mongocli config command:
mongocli config --service cloud-manager
This command creates a profile with a
custom name in the MongoDB CLI Configuration File. To use this profile
in a command, you must specify the profile with the
--profile
flag.
Run the mongocli config command:
mongocli config --service cloud-manager --profile <profile-name>
Optional: Enter your API keys.
When prompted, enter the Public API Key
and Private API Key
for your MongoDB Cloud Manager deployment.
? Public API Key: [? for help] abcdef ? Private API Key: [? for help] ************************************
Choose your default organization.
If you entered API keys, the command displays the name and ID of organizations that your API key can access.
Select your organization:
Press the
Down Arrow
andUp Arrow
keys to highlight the desired organization, orType the organization name to filter the available organizations.
? Default Org ID: [Use arrows to move, type to filter] Org1 (5e39bf1212121e685774c81c) Press
Enter
.
If you didn't enter API keys, the command prompts you to enter your
Default Organization ID
.
Choose your default project.
If you entered API keys, the command displays the name and ID of projects that your API key can access.
Select your project:
Press the
Down Arrow
andUp Arrow
keys to highlight the desired project, orType the project name to filter the available projects.
? Default Project ID: [Use arrows to move, type to filter] Project1 (5e5ebffd0c04a97009061234) Project2 (5cfacee6014b761b07f15678) Project3 (5e39bf4979358e6857741212) Project4 (5c815cc7014b768fb67e3434) Press
Enter
.
If you didn't enter API keys, the command prompts you to enter your
Default Project ID
.
Choose your default output format.
Press the
Down Arrow
andUp Arrow
keys to highlight the desired default output format:? Default Output Format: [Use arrows to move, type to filter] plaintext json OptionDescriptionplaintext
Human-readable output that includes all fields that MongoDB CLI returns.json
JSON output that includes all fields that MongoDB CLI returns.Press
Enter
.
Specify the path to the MongoDB Shell, mongosh
, on your system.
MongoDB CLI uses mongosh
path that you specify to allow you to
access your deployments. The default value is
/usr/local/bin/mongosh
. Press Enter
to accept the default
or:
Specify the path to
mongosh
on your system.? Default MongoDB Shell Path: [? for help] (/usr/local/bin/mongosh) Press
Enter
.
Verify your profile.
View the configuration file or run the mongocli config describe command to verify your profile.
Important
Your API keys are like passwords. The configuration file stores API keys in plaintext, so ensure that you secure the configuration file appropriately.
Example
The following command returns the settings in the default profile:
mongocli config describe default
The preceding command returns the following settings. For security purposes, the command redacts API keys in the output.
SETTING VALUE mongosh_path /usr/local/bin/mongosh org_id 5f1f39ffc902201990f12345 private_api_key redacted project_id 7ehf04abc10defb66c7d1234c public_api_key redacted service cloud-manager
Example
The following command returns the settings
in the profile named myMongoDB
:
mongocli config describe myMongoDB
The command returns following settings. For security purposes, the command redacts API keys in the output.
SETTING VALUE org_id 5f1f39ffc902201990f12345 private_api_key redacted project_id 7ehf04abc10defb66c7d1234c public_api_key redacted service cloud-manager
Create a profile.
This command creates a profile called
default
in the MongoDB CLI Configuration File.
Run the mongocli config command:
mongocli config --service ops-manager
This command creates a profile with a
custom name in the MongoDB CLI Configuration File. To use this profile
in a command, you must specify the profile with the
--profile
flag.
Run the mongocli config command:
mongocli config --service ops-manager --profile <profile-name>
Enter the URL you use to access Ops Manager.
? URL to Access Ops Manager: http://mms.example.com:8080
Optional: Enter your API keys.
When prompted, enter the Public API Key
and Private API Key
for your Ops Manager deployment.
? Public API Key: [? for help] abcdef ? Private API Key: [? for help] ************************************
Choose your default organization.
If you entered API keys, the command displays the name and ID of organizations that your API key can access.
Select your organization:
Press the
Down Arrow
andUp Arrow
keys to highlight the desired organization, orType the organization name to filter the available organizations.
? Default Org ID: [Use arrows to move, type to filter] Org1 (5e39bf1212121e685774c81c) Press
Enter
.
If you didn't enter API keys, the command prompts you to enter your
Default Organization ID
.
Choose your default project.
If you entered API keys, the command displays the name and ID of projects that your API key can access.
Select your project:
Press the
Down Arrow
andUp Arrow
keys to highlight the desired project, orType the project name to filter the available projects.
? Default Project ID: [Use arrows to move, type to filter] Project1 (5e5ebffd0c04a97009061234) Project2 (5cfacee6014b761b07f15678) Project3 (5e39bf4979358e6857741212) Project4 (5c815cc7014b768fb67e3434) Press
Enter
.
If you didn't enter API keys, the command prompts you to enter your
Default Project ID
.
Choose your default output format.
Press the
Down Arrow
andUp Arrow
keys to highlight the desired default output format:? Default Output Format: [Use arrows to move, type to filter] plaintext json OptionDescriptionplaintext
Human-readable output that includes all fields that MongoDB CLI returns.json
JSON output that includes all fields that MongoDB CLI returns.Press
Enter
.
Specify the path to the MongoDB Shell, mongosh
, on your system.
MongoDB CLI uses mongosh
path that you specify to allow you to
access your deployments. The default value is
/usr/local/bin/mongosh
. Press Enter
to accept the default
or:
Specify the path to
mongosh
on your system.? Default MongoDB Shell Path: [? for help] (/usr/local/bin/mongosh) Press
Enter
.
Verify your profile.
View the configuration file or run the mongocli config describe command to verify your profile.
Important
Your API keys are like passwords. The configuration file stores API keys in plaintext, so ensure that you secure the configuration file appropriately.
Example
The following command returns the settings in the default profile:
mongocli config describe default
The preceding command returns the following settings. For security purposes, the command redacts API keys in the output.
SETTING VALUE mongosh_path /usr/local/bin/mongosh ops_manager_url = https://mms.example.com:8080/ org_id 5f1f39ffc902201990f12345 private_api_key redacted project_id 7ehf04abc10defb66c7d1234c public_api_key redacted service ops-manager
Example
The following command returns the settings
in the profile named myMongoDB
:
mongocli config describe myMongoDB
The command returns following settings. For security purposes, the command redacts API keys in the output.
SETTING VALUE ops_manager_url = https://mms.example.com:8080/ org_id 5f1f39ffc902201990f12345 private_api_key redacted project_id 7ehf04abc10defb66c7d1234c public_api_key redacted service ops-manager
Next Steps
Edit your configuration file to add or modify profiles.
Set environment variables to enable easier scripting.
Enable autocomplete to see available commands and their syntax directly in your shell.