Good Place. Good Things. Good Time.

Mongodb shell

Mongodb shell

Introduction to Mongodb shell

MongoDB shell is an interactive interface created by using javascript by using which you can perform various tasks and functions that are related to MongoDB instances. This shell works just like a command line tool to interact with the Mongo database an proves to be more beneficial when you have to perform certain changes or manipulate the data of the Mongo or used by admin to perform various tasks such as maintaining the instances of the database, its access and privileges. This is the client which is used by default for the MongoDB database servers. All the things are console based whether there be any input to be given or output to be received as it is a command-line interface also referred as ( CLI ).

In this article, we will be looking at a tool that is generally very useful when working with small sets of data called Mongo shell. We will study its features, installation guidance, query running, data manipulation, admin operations, commands used, etc.

Mongosh – This is the new mongo shell introduced by Mongo DB team which brings certain enhancements in it including embeddability and extensibility which makes it capable to be used even inside some other products such as Visual Studio Code Editor.


Whenever we install the MongoDB server at that time the Mongo shell or MongoDB client is automatically installed internally. Still if you want to externally install MongoDB shell then you can make the use of this link for downloading it from the center. Over there, you will find the archive files of different versions and packages. You can select whichever version of MongoDB shell you want and its corresponding package and download the archive file of the same from above link. Further, you can copy that archive file to whichever location you want to keep it inside your file system.

MongoDB shell is provided for multiple platforms. All the supporting platforms or operating systems for which you can download the MongoDB shell are as listed below –

  • Mac OS
  • Linux
  • Windows

Connecting to Mongo Database

The next step in using the MongoDB shell after you have downloaded and installed it, is to connect with the MongoDB server. After this, you will be all set to use the MongoDB database and manipulate the data or perform whatsoever operations you want on the database.

Before you go for establishing a connection it is required that your Mongo DB server is aleady running. For beginning the server, you can fire the following command on the command prompt or shell –

net start mongodb

The execution of above command gives the following output –

Mongodb shell output 1

To run the MongoDB shell, you need to fire the following command –


After executing this command, you can see the following kind of output on the command prompt –

Mongodb shell output 2

After this output, you can see that you will be inside the mongo shell.

Alternatively, you can also run the mongo DB shell without the use of command prompt. For this you will have to go to the location where you have your executable mongod and mongo files. After that, do a double click on both of them to make them run and then you will obtain the result similar to as shown above.

Port Settings of Mongodb shell

All the above process can only execute properly when your MongoDB server will run on its default port address which is 27017. In case if your Mongo server is running on the port address other than the above-mentioned port then you have to use the command which will explicitly mention the same by using the following statement –

Mongo –port 28018

Which will give the same output as above.

Special case of having a remote server

When your Mongo DB server is not present in the same device or in the same local network that is on localhost and is situated remotely somewhere then above commands won’t work. There is a provision of -host option which can be used in all the above commands to mention the host address where your server is present. For example, in the below statement is the host where my server is present. So, I will be using following command –

Mongo –host –port 28010

Which will give the output similar to the above just the address details will change.

Basic Commands of Mongodb shell

Let us now have a look over some of the most used commands and that will help you to get started with using the MongoDB shell.

In order to check your current database where you are working, you can fire the following command –


The execution of above command gives the following output in my case –

output 3

In order to use a different database, you can make the use of the use command as shown below –

use educba_organisation

The output of above command is as shown below –

output 4

Further, if you want to create the collections inside the current database you are using and insert some sort of data in the collection then you can make the use of the insertOne() function. We need to use this function by mentioning the db which will refer to the database that you will be using at current moment, name of the collection and then the insertOne() method to add a new document record in the collection. Let us consider one example where I want to add a new record to a collection whose name is articles. I can do this by using the following query statement –

db.articles.insertOne({name : “Mongodb Shell”});

The execution of above query statement gives the following output shown in the below image –

output 5

In order to retrieve or fetch the data that is present inside the collection, you can make the use of find() method that returns a cursor containing all the records or documents that are present inside the collection. Further, you can make the use of forEach() loop to iterate over the cursor and printjson as the parameter to it to print all the records of the cursor. In our example, we can make the use of following statement to fetch and print the contents of the articles collection –


The output of above command is as shown below –

output 6


The MongoDB shell automatically installs when you install your MongoDB server. Still, you can download and install it externally. This article gives you a brief overview of installation, connecting to the database, and the basic commands that are used in Mongo DB.

 Recommended Articles

This is a guide to Mongodb shell. Here we discuss the features, installation guidance, query running, data manipulation, admin operations, commands used, etc. You may also have a look at the following articles to learn more –

  1. Data Modeling in MongoDB
  2. MongoDB Date Query
  3. MongoDB Sharding
  4. MongoDB Skip()

The post Mongodb shell appeared first on EDUCBA.

Read original article here: Mongodb shell

Read original article here: Mongodb shell

Disclaimers and Denial of responsibility..!

Denial of responsibility! is an automatic aggregator of all media around the world. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – . The content will be deleted within 72 hours.
You might also like
Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.