• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:9 mins read
You are currently viewing Working with MongoDB mongosh Shell

MongoDB shell command mongosh is designed to provide an enhanced user experience to work with MongoDB database from the command line. The mongosh is a new shell that is intended to replace the existing mongo shell and provides several new features, including better syntax highlighting, improved error messages, and enhanced auto-completion.


The mongosh shell allows users to connect to a MongoDB instance and perform various operations, including querying and modifying data, creating and managing collections, and running administrative tasks.

In this article, we will see how to install a mongosh shell on Windows and use it to interact with the MongoDB database from the command line. Some common commands you can perform from the mongosh shell include.

  • show dbs: displays a list of all available databases.
  • use <database>: switches to the specified database.
  • show collections: displays a list of all collections in the current database.
  • db.<collection>.find(): retrieves all documents in the specified collection.
  • db.<collection>.insertOne(): inserts a new document into the specified collection.

1. Install MongoDB mongosh Shell

First, check whether mongosh.exe is installed by checking in its installation directory. If the mongosh.exe is not found then we have to install it, to install go to MongoDB’s official website, select the product option from the header menu and it will show the different other options for different purposes as shown in the image. Now, we see the shells option highlighted on the screen which is available in the Tools option.

Install MongoDB Shell

Once, we click on the shell option, then it will redirect us to the Download page as displayed on the image. Click on the Download Now button to download the MongoDB mongosh shell. 

Install and use MongoDB Shell

The MongoDB shell download page is now open and here we can see the installer file of the new MongoDB shell, select the version and the platform according to our requirement and finally select the Download option. Once the download completes, unzip and keep the files in the MongoDB bin directory.

Extract the zip folder by selecting the destination directory as shown below. 

Install and use MongoDB Shell

Here, we can see the mongosh.exe application in our system. In my system, I have placed it on the path mongosh-1.6.1-win32-x64.zip\mongosh-1.6.1-win32-x64\bin.

mongosh shell

When we click the mongosh.exe application to open, it will launch the mongosh shell as in the following screen. All we need to press enter to establish the connection with MongoDB.

Install and use MongoDB Shell

We can see the connection string with further information by following the above step. Now, we are in the mongosh shell and mongosh shell ready to perform the MongoDB operations.

Additionally, this link can be used to install MongoDB Shell.

Now, let’s connect to a MongoDB instance and perform show databases & collections, switch databases, and execute javascript.

2. Show Databases and Collections in MongoDB Shell

As we have completed the MongoDB mongosh installation process in the above section. Let’s connect to the database and run some basic commands from the mongosh shell. First, get the existing databases by using the show dbs command.

# List all databases
show dbs

The above command lists all the databases here.

Install and use MongoDB Shell

Similarly, we can get all the collections in the database by using the following command of MongoDB.

# List all collections
show collections

Hence, the output listed the collections below.

mongosh Shell

3. Switched the Database in Mongosh Shell

You can switch to the desired database in the mongosh shell by using the switch command. The following command was used to switch to the provided database or to create the database if it didn’t already exist.

# Switch the database
use StudentDatabase

Here, we have switched to the studentDatabase.

4. Execute JavaScript Code in Mongosh Shell

Additionally, we can perform limited JavaScript code within the context of the MongoDB shell. To execute a simple JavaScript REPL that gets the length of the specified string, we can enter the following command in the MongoDB shell.

# Execute JavaScript Code

The above line uses the built-in length property of the string object to get its length which is 7 as displayed in the following mongosh shell screen.

Install MongoDB mongosh Shell

If you have JSON files and wanted to get into the MongoDB collection, use the mongoimport command to import JSON file.

5. Conclusion

In conclusion, MongoDB mongosh shell support multiple languages, improved interface, and useful features making it a great tool for anyone working with MongoDB in their development projects. I hope you have succesfully installed mongosh shell and executed simple commands of MongoDB in it.