Tutorialsteacher

Follow Us

MongoDB Shell

MongoDB Shell is the quickest way to connect, configure, query, and work with your MongoDB database. It acts as a command-line client of the MongoDB server.

The MongoDB Shell is a standalone, open-source product and developed separately from the MongoDB Server under the Apache 2 license. It is a fully functional JavaScript and Node.js 14.x REPL for interacting with MongoDB servers.

MongoDB Shell is already installed with MongoDB. You can find it in the installation directory where you installed MongoDB. By default, it is "C:\Program Files\MongoDB\Server". Open the installation folder and appropriate version folder and go to the "bin" folder. Here, "mongo.exe" is MongoDB shell. Click on it to open the MongoDB shell, as shown below.

MongoDB Shell

New MongoDB Shell - mongosh

In the installation folder, if you find mongosh.exe instead of mongo.exe then you already have a new MongoDB shell. If you don't find it, then you need to install it separately.

The new MongoDB Shell mongosh has some more features than old shell mongo such as intelligent autocomplete and syntax highlighting, easy to understand error messages, formatting feature to present the output in a readable format, etc. However, all the commands will be executed in mongosh as well as mongo shell.

Install mongosh

To install the new MongoDB shell (mongosh), visit www.mongodb.com and click on Product menu -> Tools -> Database Shell, as shown below.

Download New MongoDB Shell

On the MongoDB Shell page, click on the Download button to download the shell.

Download New MongoDB Shell

This will take you to a page where you can select a version, platform, and package to download, as shown below.

Download New MongoDB Shell

Click on the Download button to download the installer file.

Now, click on the downloaded installer file to start the installation wizard, as shown below.

Install New MongoDB Shell - mongosh

Click Next to go to next step shown below.

Install New MongoDB Shell - mongosh

Here, uncheck the checkbox if you want to install shell for all users on your local machine and click Next.

Install New MongoDB Shell - mongosh

Click on the Install button to start the installation. It should quickly install it.

Install New MongoDB Shell - mongosh

Once installation completes, click the Finish button to close the wizard.

This should have installed mongosh in "C:\Program Files\mongosh" folder on Windows, as shown below.

New MongoDB Shell - mongosh

Click on the mongosh.exe to open a new MongoDB shell, as shown below.

New MongoDB Shell - mongosh

Press Enter to start the shell, as shown below.

New MongoDB Shell - mongosh

Alternatively, open a new command prompt on Windows and write mongosh and press Enter. It will open the same MongoDB shell.

Execute MongoDB Commands

You can execute MongoB commands for CRUD operations on MongoDB shell (mongo or mongosh). For example, execute the "shows dbs" command to see all the databases on the connected MongoDB server.

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

Use the "db" command to check the current database.

> db
test

Run the .editor command to execute multi-line commands. Press Ctrl + d to run a command or Ctrl + c to cancel.

MongoDB shell is JavaScript and Node.js REPL, so you can also execute limited JavaScript code.

> "Hello".length
5

Press Ctrl + c twice to exit from the MongoDB shell.