MongoDB Shell: Your Command-Line Interface to MongoDB

Learn how to utilize the MongoDB Shell, a powerful command-line tool for interacting with MongoDB databases. Discover the essential commands and features that allow you to connect, query, and manage your MongoDB data effectively. This guide will equip you with the skills to navigate and leverage the MongoDB Shell for your database tasks.



MongoDB Shell

MongoDB Shell is a command-line client for connecting to and working with MongoDB databases. It is a standalone, open-source tool developed under the Apache 2 license, and provides a JavaScript and Node.js REPL environment.

Accessing MongoDB Shell

The MongoDB Shell (mongo.exe) is installed with MongoDB. By default, you can find it at C:\Program Files\MongoDB\Server\bin\mongo.exe. Open this file to start the shell.

New MongoDB Shell - mongosh

If you see mongosh.exe instead of mongo.exe, you have the new MongoDB Shell. If not, you need to install it separately.

Features of mongosh

  • Intelligent autocomplete and syntax highlighting
  • Readable error messages
  • Output formatting features

Install mongosh

To install mongosh:

  1. Visit MongoDB and navigate to Product -> Tools -> Database Shell.
  2. Click Download to select version, platform, and package, then download the installer.
  3. Run the installer and follow the prompts. Click Next and adjust settings as needed.
  4. Click Install and then Finish once installation is complete.

After installation, mongosh will be located in C:\Program Files\mongosh. Run mongosh.exe to start the new shell.

Using MongoDB Shell

To execute commands, simply type them into the shell:

MongoDB Commands

> show dbs
admin 41 kB
config 111 kB
local 41 kB

> db
test

> .editor
"Hello".length
5

Use Ctrl + d to run multi-line commands and Ctrl + c to cancel. Press Ctrl + c twice to exit the shell.