EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Commands
Secondary Sidebar
MongoDB Tutorial
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove

Related Courses

MongoDB Certification Course

Oracle Certification Course

All in One Data Science Course

SQL Training Course

Oracle DBA Course

MS SQL Certification Course

MongoDB Commands

By Priya PedamkarPriya Pedamkar

mongodb commands

Introduction to MongoDB Commands

MongoDB is a cross-platform, document-oriented, open-source database management system that provides, high availability, high performance, and easy scalability. It is one of the NoSQL databases, highly used for big data applications and other complex job processing for the data that doesn’t go well with the relational database. Instead of using the relational database concept of storing data in the form of the table, MongoDB architecture is made up of collections and documents. Here we discuss the MongoDB commands.

Why MongoDB Commands?

  1. It can easily control the data which is placed globally, 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. No downtime, if the application is scaled.

Features

  1. MongoDB command uses a master-slave replication concept. To prevent database downtime, this replica feature is proved to be an essential feature.
  2. This database can run over multiple servers; hence data is duplicated over multiple servers. The result of which it’s a great advantage in case of hardware failure.
  3. MongoDB command comes with the auto-sharding feature, in which process 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 existed one.

MongoDB command-Create Database

To check the current database now:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Create Database 2

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

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,171 ratings)

2. Drop Database

 MongoDB command -Drop Database

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

3. Create Collection

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

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

4. Drop Collection

MongoDB command -Drop Collection

Drop Collection 2

5. Insert Document

Insert() or save() method is used to insert data into any database collection.

 MongoDB command -Insert Document

Here “mycol” is the collection name. If the collection doesn’t exist, then MongoDB command will create the database collection and then it will get inserted.

6. Query Document

Querying collection is done by find() method.

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

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 type. 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 inefficient way

Indexing

Advanced Commands of  MongoDB

1. Aggregate ()

This MongoDB command helps in processing 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 that have 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 rest others are the secondary node. All write operations remains with the primary node.

Let’s see; standalone MongoDB instance gets converted into a replica set.

Here are steps for that:

Close already running MongoDB server.

Now Start the MongoDB server by specifying — replSet option.

Syntax:

Syntax

3. Create & restore Backup

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

Create & restore Backup

To restore the backup, a mongorestore command is used.

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 need to go to your bin directory of MongoDB installation and run mongostat.

Monitor Deployment

Tips and Tricks to use MongoDB commands

  • Pre-allocate space: When you know, your document is going to grow up 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, its good habit of using regex. Like:

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

  • Prefer Odd No. of Replica Sets: An easy way to add redundancy and increase read performance is by using replica sets. Data is replicated between all nodes, and in case of primary node failure. Voting takes place between themselves, and the primary node is elected. Using the odd number of the replica will make voting easier in case of failure.
  • Secure MongoDB using a firewall: As MongoDB itself doesn’t provide any authentication, it’s better to secure it with firewall and mapping it to the correct interface.
  • No joins: As we know, joins are not supported by MongoDB. To retrieve data from more than two collections, one needs to write more than one query. And if the schema is not well organized, writing query may go hectic. 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. Important VBA Commands- Data Filter and Dynamic Update
  5. Guide to MongoDB creates Index.
  6. Top 11 MongoDB List Collections
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (14 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (7 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • 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

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

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

Let’s Get Started

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