EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial Mongodb shell

Mongodb shell

Updated March 8, 2023

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 ).

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

Installation

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 –

mongo

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 educba.sample.com is the host where my server is present. So, I will be using following command –

Mongo –host educba.sample.com –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 –

db

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 –

db.articles.find().forEach(printjson)

The output of above command is as shown below –

output 6

Conclusion

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()
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle2000+ Hour of HD Videos | 80 Learning Paths | 400+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more