EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Commands
 

MongoDB Commands

Priya Pedamkar
Article byPriya Pedamkar

mongodb commands

Introduction to MongoDB Commands

MongoDB is a cross-platform, document-oriented, open-source database management system with high availability, performance, and scalability. MongoDB, a NoSQL database, finds extensive use in big data applications and other complex data processing tasks that do not align well with the relational database model. Instead of using the relational database notion of storing data in tables, MongoDB architecture is built on collections and documents. Here we discuss the MongoDB commands.

 

 

Why MongoDB Commands?

  1. It can easily control global data, ensuring fast performance and compliance.
  2. It provides a flexible data model. This goes with the case where the app needs to be built from scratch or the case of updating a single record.
  3. Scaling the application ensures that there is no downtime.

Features

  1. MongoDB command uses a master-slave replication concept. To prevent database downtime, this replica feature is essential.
  2. This database can run over multiple servers; various servers duplicate the data. The result of which it’s a significant advantage in case of hardware failure.
  3. MongoDB command comes with the auto-sharding feature, which distributes data across multiple physical partitions known as shards. The result of which automatic load balancing happens.
  4. It’s schema-less. Hence more efficient.

Basic of MongoDB Commands

1. Create database

In MongoDB use, DATABASE_NAME is used to create a database. If this name database doesn’t exist, it will get created, and else it will return the existing one.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

MongoDB command-Create Database

To check the current database now:

Create Database 2

By default, the MongoDB command comes with the database name “test.” Suppose you inserted a document without specifying the database; MongoDB will automatically store it in a “test” database.

2. Drop Database

 MongoDB command -Drop Database

If the database is not specified, it will delete the default database, “test.”

3. Create Collection

To create a collection, the MongoDB command used is: db.createCollection(name, options)

Here, the name is the Collection’s name & options are a document used to specify the Collection’s configuration. Though the “Options” parameter is optional, it’s good to provide it.

4. Drop Collection

MongoDB command -Drop Collection

Drop Collection 2

5. Insert Document

To insert data into a database collection in MongoDB, you can use the “insert()” or “save()” method.

 MongoDB command -Insert Document

Here “mycol” is the collection name. If the Collection doesn’t exist, then the MongoDB command will create the database collection, which will be inserted.

6. Query Document

Querying Collection is done by the find() method.

As the find() method will show the findings in a non-structured way, a structured pretty() method is used to get the results.

MongoDB command -Query Document

Intermediate MongoDB Commands

1. Limit()

This MongoDB command limits the no. of records need to use in MongoDB. The argument of this function accepts only number types. The argument is the number of the Document that needs to be displayed.

Limit()

2. Sort()

This is to the records of MongoDB. 1 & -1 are used to sort the documents. 1 is for ascending, whereas -1 is for descending.

Sort()

3. Indexing is the concept that helps MongoDB to scan documents in an inefficient way

Indexing

Advanced Commands of  MongoDB

1. Aggregate ()

This MongoDB command helps process the data, which returns the calculated result. This can group values from multiple documents together.

Aggregate

2. Replication

Replication in MongoDB is achieved using a replication set. A replica set is a group of MongoDB processes with the same dataset. Replica set provides:

  1. High availability
  2. Redundancy hence faults tolerant/disaster recovery.

In replica, one node is the primary node, and the rest are the secondary node. All write operations remain with the primary node.

Let’s see; you can convert a standalone MongoDB instance into a replica set.

Here are the steps for that:

Close is already running the MongoDB server.

Now Start the MongoDB server by specifying — replSet option.

Syntax:

Syntax

3. Create & restore Backup

To create the Backup, the mongodump command is used. The server’s data will be dumped into a dump directory(/bin/dump/). Options are there to limit the data.

Create & restore Backup

To restore a backup in MongoDB, you would use the “mongorestore” command.

Create & restore Backup 2

4. Monitor Deployment

To check the status of all your running processes/instances, a mongostat command is helpful. It tracks and returns the counter of database operations. These counters include inserts, updates, queries, deletes, and cursors. This MongoDB command is beneficial as it shows your status about low running memory, some performance issues, etc.

You must go to your MongoDB installation bin directory and run mongostat.

Monitor Deployment

Tips and Tricks to Use MongoDB Commands

  • Pre-allocate space: When you know your Document will grow to a certain size. This is an optimization technique in MongoDB. Insert a document and add a garbage field.
  • Try fetching data in a single query.
  • As MongoDB is, by default, case sensitive.

Example:

db.people.find({name: ‘Russell’}) &

db.people.find({name: ‘russell’}) are different.

While performing a search, it’s a good habit to use regex. Like:

db.people.find({name: /russell/i})

  • Prefer Odd No. of Replica Sets: Using replica sets is an easy way to add redundancy and enhance read performance. All nodes replicate the data, and it can be retrieved in case of a primary node failure. They vote amongst themselves and elect a primary node. Using the odd number of the replica will make voting more accessible in case of failure.
  • Secure MongoDB using a firewall: As MongoDB itself doesn’t provide any authentication, it’s better to secure it with a firewall and mapping it to the correct interface.
  • No joins: MongoDB, a NoSQL database, does not support joins. One must write multiple queries to retrieve data from more than two collections. Writing queries can become hectic if the schema is not well organized. This may result in the re-designing of the schema. It’s always better to spend some extra time to design a schema.

 Conclusion

MongoDB commands are the best practice solution to maintain high availability, efficient and scalable operations, which is today’s business demand.

Recommended Articles

This has been a guide to MongoDB commands. Here we have discussed basic, intermediate, and advanced MongoDB commands and tips and tricks to use those commands. You may also look at the following article to learn more –

  1. Tableau Commands
  2. Adobe Photoshop Commands
  3. Cheatsheet SQL (Commands, Free Tips, and Tricks)
  4. Guide to MongoDB creates Index.
  5. Top 11 MongoDB List Collections

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

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
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 Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW