AnnouncementIntroducing MongoDB 8.0, the fastest MongoDB ever! Read more >>Introducing MongoDB 8.0, the fastest MongoDB ever! >>

Command Line Tools

Command line tools are quite efficient because of the simplicity of execution and less usage of system resources. Command line tools let you perform various actions like creating, managing, and deleting files and directories; configuring system and network settings; setting path; installing/uninstalling software programs; monitoring system performance; and so on. Before learning about command line tools and the interface, let’s discuss the basic unit of communication between a user and the computer: the shell.


Table of contents

What is a shell?

A shell is a program or environment to communicate with an operating system or execute computer programs. The shell can be graphical (like Windows Explorer/macOS Finder) or text-based (like command prompt, zsh, or bash). A command-line shell eliminates the overhead of GUI, enhancing productivity.


Terminal vs shell

Although the terms “terminal” and “shell” are often used interchangeably, there is a difference between the two. A terminal is the interface that allows users to type commands and view output. It could be a window or any application.

The shell runs within a terminal and is a command interpreter. It interacts with the operating system and executes the commands and scripts written on the terminal.

What is a command line interface?

A command line interface (CLI) is a text-based user interface provided by the shell, where users type text commands to interact with the computer. The commands can be entered through a terminal window, console, or command prompt. For instance, on a Windows Operating System, you can use Win+R or cmd.exe to open the terminal and type commands for the required actions. A command line interface is more repeatable and less error-prone for system administrators and developers for automating complex tasks, executing scripts, and batch processing. Some popular commands are grep, git, mkdir, head, kill, and zip.

What are command line arguments?

Some commands require parameters (arguments) to be specified while executing the command — for instance, copying contents from one file to another, where you have to mention the source file and destination file names, along with the cp command:

cp <source_file_name> <destination_file_name>

Here, <source_file_name> and <destination_file_name> are the command line arguments provided to the command line interface for the ‘cp’ command to execute successfully.

Why do we need a command line tool?

Although the UI seems more intuitive and easy to use, when it comes to efficiency, automation, control, and speed, the command line interface takes the front seat. Command line tools provide a better user experience for advanced programmers and admins, with intuitive features like auto-completion and scripts to automate frequent tasks.

  • For advanced users, it provides a quick way to execute commands without the overhead of navigating through and pressing multiple buttons to do simple tasks.

  • For remote connections using different protocols like SSH or TCP, it is essential to have command line tools.

  • Version control tools like Git use command line tools to manage projects and track changes.

  • Command line tools are most often the major means to interact in a server environment to communicate between an application and its services.

Common command line tools

Most operating systems, database management systems, web servers, version control systems, cloud services, container services, programming languages, text editors, and development, network, monitoring, performance, and security tools provide a command line interface that is accessed via a terminal window. Some commonly used command line tools (in a POSIX system) are:

  • ls — lists all the files and directories in a directory.
  • cd — changes the current working directory.
  • cp — copies files or directories to another location.
  • ssh — logs into a remote server securely and executes commands for the remote server.

These can be used in scripts, which can then be tested, distributed, and customized, making them reusable components for admins and developers.


Operating system

Unix-like operating systems (like Linux) and macOS use the shell, where we can use the command line tools like ls, cd, mkdir, and many more. Some common Unix shells are bash and zsh.

Microsoft Windows uses “Command Prompt” and PowerShell as the command line interface.


Database management systems

Database management systems like MongoDB, MySQL, Oracle, and PostgreSQL provide CLI tools to administer and query data.

MongoDB Atlas, MongoDB’s intelligent data platform, provides a CLI tool, “mongocli,” to interact with Atlas resources from the command line interface. MongoDB also provides database utilities and other CLI tools, like mongosh (MongoDB Shell), and utilities like mongodump, mongorestore, and many more to effectively manage, monitor, and back up database deployments through a command line interface.


Web servers

Web servers provide command line tools for configuration and server management. Examples are apachectl for Apache and nginx for Nginx.


Version control systems

Git, one of the most popular version control systems, has a command line interface to manage source code.


Cloud service providers

Popular cloud service providers like AWS (AWS CLI), Azure (az), and Google Cloud (gcloud) provide command line tools to automate cloud operations and configure and manage cloud infrastructure and resources. These cloud providers can leverage MongoDB Cloud Manager to monitor, manage, and automate MongoDB deployments on their infrastructure.

MongoDB Shell

Mongo Shell is a Javascript-based command line interface. It is the default shell provided by MongoDB. Although you get MongoDB Shell with MongoDB server installation as a binary, you can download and install MongoDB Shell separately too. MongoDB Shell can be used to perform CRUD (create, read, update, and delete) operations, as well as administrative tasks via CLI. The newer version, mongosh, provides many benefits over the older MongoDB Shell, like improved syntax highlighting, logging, and command history. You can use the shell for all the MongoDB deployments, like on-premise, Community edition, Enterprise edition, Advanced, and Atlas.


MongoDB Shell examples

While CLI and Shell both let you run commands through a text interface, Shell provides a Javascript interface, which you can use to query a particular database. You can learn more about how to query a database using mongo shell in the MongoDB blog. Some common commands are given below:

  • To list the databases — show dbs
  • To list collections — use <db_name>; and then show collections
  • Display all the documents — db.<db_name>.find()
  • Display only the first few documents — db.<db_name>.find().limit(num_of_docs_to_show)

MongoDB database tools

MongoDB database tools are a set of command line tools or utilities for a MongoDB deployment. For example, the mongostat command line tool gives a brief overview of the status of a currently running mongod or mongos instance. mongostat provides real-time monitoring and analyzes database activity. Similarly, mongotop is another utility that tracks the time an instance spends on read and write operations. These are system commands and run on the command line interface and not on the Mongo Shell. You can download these utilities separately (from MongoDB version 4.4).

List of command line binary utilities provided by mongodb.

List of command line binary utilities provided by mongodb — mongotop, mongofiles, mongostat, mongoexport, mongoimport, mongodump, bsondump, mongorestore.

MongoDB Command Line Interface (MongoDB CLI)

The Shell provides access to retrieve and store data for various types of deployments. However, MongoDB also provides a command line interface to manage your deployments, like MongoDB Atlas CLI, MongoDB Ops Manager, and MongoDB Cloud Manager.

Configuring Atlas CLI for non-programmatic use List of available MongoDB command line tools


To use the MongoDB CLI, you need to install and configure it. For any of the services, you can use mongocli auth login (authenticate with Atlas) or the mongocli config (configure and manage user profiles) command. Internally, upon executing these commands, MongoDB creates a configuration file (config.toml) to store the settings. You can also specify configuration settings by using environment variables for the ease of scripting. For instance, you can set the MongoDB service type used with the MCLI_SERVICE environment variable.


MongoDB Atlas CLI

MongoDB Atlas is an integrated cloud-based data platform provided by MongoDB. The Atlas CLI is built for managing MongoDB Atlas operations. While Atlas has a rich and powerful UI, you can interact with your Altas deployments and Atlas Search using the CLI commands.

Atlas CLI is intuitive and provides short commands for actions that would take multiple clicks on the UI. To manage Atlas from the command line, you can install the Atlas CLI and run the setup, which quickly creates a cluster for you with the default settings. You can also run multiple commands as a single script to automate tasks and set triggers and events. Atlas CLI is suitable for batch processing commands.


MongoDB CLI for Cloud Manager and Ops Manager

The CLI for Ops Manager and Cloud Manager helps you manage your Ops Manager projects and clusters deployed on–premise or on cloud. Cloud Manager is a monitoring and management platform for cloud-hosted applications, and Ops Manager manages the on-premise and self-hosted deployments. The CLI enables you to create, update, and manage MongoDB clusters, manage backup, monitor configuration, and scale applications for both types of deployments.

To access the cloud manager commands, use the mongocli cloud-manager along with the command and the options you want to use.

To access the ops manager commands, use the mongocli-ops-manager tool along with the command and the options.

Using Atlas command line tools

You can manage Atlas resources via the command line interface in a programmatic or non-programmatic manner. Having programmatic access lets you automate workflows and manage database resources using APIs or scripts. In short, manage your infrastructure as code via the command line interface, as opposed to the manual interaction via the user interface or command line tools, in a non-programmatic access.

To manage Atlas from the command line interface, you must first install the Atlas CLI and the mongo shell. Note that you should have an Atlas account (free or paid) for your organization or project to be able to access it from the command line.

If you do not have an Atlas account, you can set up one using the atlas setup command line tool. The setup command also creates a free cluster with default settings and prompts you to change any settings that you want to.

To connect to your Atlas account using command line tools, you can use one of the following methods.


Non-programmatic use

This approach includes manual interaction with the database for creation of clusters, querying, configuring cluster setting, monitoring performance, and managing backups. To be able to use the command line tools, you should log in using your Atlas credentials and a one-time authentication token using the command atlas auth login.

As a next step, you should add the host IP address to the IP access list of your organization. You can do this using the Atlas UI from the Network Access tab of your project:


Configuring Atlas CLI for non-programmatic use.

Using the Atlas UI to configure Atlas CLI for non-programmatic use

You can view the IP Access List using the command line tool atlas accessLists list [options] . You can view the options and parameters in the documentation page.


Programmatic use

For programmatic use, you should run the atlas config init command. Atlas provides:

  • Atlas Administration API to programmatically manage organization-level tasks like managing organization, projects, and account settings.
  • Atlas App Services Admin API to manage clusters, users, roles, security settings, and other database-level administration tasks.

To use these APIs, you must have the API keys — a Public Key and a Private Key. You can create the API keys from the Atlas UI using the Organization Access Manager page:

Configuring Atlas CLI for programmatic use

Using the Atlas UI to configure Atlas CLI for programmatic use


You can also create API keys using the command line interface by using the following command:

atlas organizations apiKeys create [options]

Atlas CLI provides short, intuitive commands to manage your Atlas database deployments and search. To understand how to use a particular command, you can use the --help flag with the command name to learn more about it. For example, for help with the setup command, you should use atlas setup --help.

If you want to set up Atlas with all the default settings without asking for confirmation on changes in any of the settings, you can use the force flag:

atlas setup --force

To list all the clusters in your account, use:

atlas cluster ls

To create a new cluster, use the create command:

atlas cluster create [cluster name] [options]

Using the help command gives you examples to create a cluster on different environments with the given options, like projectId, provider, region, members, tier, MongoDB version, and disk size.

# Deploy a free cluster named myCluster for the project with the ID 5e2211c17a3e5a48f5497de3:
atlas cluster create myCluster --projectId 5e2211c17a3e5a48f5497de3 --provider AWS --region US_EAST_1 --tier M0

To fetch details of a cluster in a JSON format, use the get command:

atlas cluster get <cluster-name> -o json

You can also output the details to a file as:

atlas cluster get <cluster-name> -o json > [describe_cluster_name].json

Using the information in the file, you can create another new cluster, as well:

atlas cluster create <cluster_name> -file [describe_cluster_name].json

To delete a cluster, simply use the delete command:

atlas cluster delete <cluster_name>

If you want to delete the cluster without seeing the confirmation message, use the force option. Similar to creating cluster, you can create a database user as:

atlas dbuser create -username <user_name> -password <default_pwd> -role <access_role>

You can also create project in a similar manner:

atlas project create <project_name>

You can view a list of the Atlas command line interface commands in the documentation page or simply type the “atlas” command from the CLI to get a list of available commands.

Screenshot of list of available commands on Atlas CLI

A snapshot of listing the available Atlas CLI commands upon typing the atlas command on the CLI


The list is quite exhaustive and you can use the --help menu to use the commands correctly.

Other command line tools

You can install the Atlas Kubernetes Operator by executing a single command from the CLI.

atlas kubernetes operator install [options]

You can specify options like kubeContext, kubeconfig, operatorVersion, and others as needed.

MongoDB provides a command line tool mongosync for cluster to cluster sync. mongosync migrates data from one cluster to another and keeps the clusters in continuous sync.

You can also use the mongocli iam for Identity and Access Management in Cloud Manager and Ops Manager.

Summary

Command line tools are a great way to enhance productivity, automate tasks using scripts, and perform multiple tasks using a single script or command. MongoDB provides an exhaustive set of tools for managing various MongoDB deployments in a more efficient way through the command line interface. MongoDB follows a three-step process to configure the CLI — creating API keys, creating a project, and creating a profile.

FAQs

Why do I need command line tools?

Command line tools are often a faster way to interact with the computer and execute programs. These tools can do anything, from copying and listing files or directories to searching specific files or words, and much more. For example, the MongoDB CLI provides a powerful interface to create, manage, and automate all your MongoDB deployments.

What are command line tools?

Command line tools are text-based commands used in a command line interface to execute programs, scripts, and workflows. Some popular command line tools are ls (list), cd (change directory), and mkdir (make directory).

What are MongoDB command line tools?

MongoDB provides a command line interface for all of its deployments — on-premise or on cloud, through the various platforms, namely MongoDB Ops Manager, MongoDB Cloud Manager, and MongoDB Atlas. The command line tools provided by MongoDB are essentially commands that help you create, manage, and automate your deployments. For example, you can create a new cluster from the command line interface using a single command atlas cluster create.

How to install command line tools

Most operating systems provide package managers to easily install the command line tools. A package manager also handles dependencies and updates. Some common package managers include apt, yum, brew, and choco. You can also install the command line tools manually from the tool’s official website, if the tool does not have its own package manager. For example, you can install or update the MongoDB CLI from our official website page.

What is an example of a command line tool?

Some common command line tools are ls, cd, grep, help, and mkdir.