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?
- It can easily control the data which is placed globally, ensuring fast performance and compliance.
- 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.
- No downtime, if the application is scaled.
Features
- MongoDB command uses a master-slave replication concept. To prevent database downtime, this replica feature is proved to be an essential feature.
- 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.
- 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.
- 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.
To check the current database now:
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.
2. 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
5. Insert Document
Insert() or save() method is used to insert data into any database collection.
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.
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.
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.
3. Indexing is the concept that helps MongoDB to scan documents inefficient way
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.
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:
- High availability
- 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:
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.
To restore the backup, a mongorestore command is used.
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.
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 –
- Tableau Commands
- Adobe Photoshop Commands
- Cheatsheet SQL (Commands, Free Tips, and Tricks)
- Important VBA Commands- Data Filter and Dynamic Update
- Guide to MongoDB creates Index.
- Top 11 MongoDB List Collections
4 Online Courses | 2 Hands-on Projects | 22+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses