MongoDB Performance And Tools Used In MongoDB Database Development

MongoDB Performance and Tools – MongoDB Database Performance tutorial from Coding compilerIn this tutorial, we learn about performance and some of the tools used in MongoDB development. We are going to discuss the most important aspects of each topic. Let’s start leaning.

MongoDB Performance And MongoDB Development Tools

Here we learn about MongoDB performance and MongoDB development tools.

MongoDB Index

First, we will introduce a special collection  system.indexes that holds all the index information in our database. The role of indexes is basically the same as in relational databases in MongoDB: Helps improve the performance of queries and sorts. Create an index with  ensureIndex :

// where "name" is the field name
db.unicorns.ensureIndex({name: 1});

To delete an index  dropIndex:

db.unicorns.dropIndex({name: 1});

You can create a unique index, which requires setting the second parameter  unique to  true:

db.unicorns.ensureIndex({name: 1},
    {unique: true});

The index can be embedded in the field (again, with a dot) and any array fields. We can create a composite index like this:

db.unicorns.ensureIndex({name: 1,
    vampires: -1});

The order of the indexes (1 ascending, -1 descending) has no effect on the single-key index, but it will be different when using composite indexes, such as when you use more than one index to sort.

Read the indexes page for more information on the index.

Related MongoDB Tutorials

What is MongoDB Tutorial For Beginners
MongoDB Basics Tutorial
MongoDB Update Tutorial
MongoDB Query Tutorial
MongoDB Data Modelling Tutorial

MongoDB Index

Need to check if your query uses the index, you can use the  explain method:

db.unicorns.find().explain()

The output tells us that we are using  BasicCursor (meaning no index), 12 objects are scanned, how much time is used, what index is used, and if there is an index, there will be other useful information.

If we change the query index statement and query an indexed field, we can see that it  BtreeCursor is used as an index in the fill request:

db.unicorns.find({name: 'Pilot'}).explain()

MongoDB Replication

Replication of MongoDB is similar in some respects to replication of relational databases. All production deployments should be replica sets, ideally three or more servers maintain the same data. The write operation is sent to a single server, the primary server, and then asynchronously replicated from it to all of the secondary servers. 

You can control whether read operations are allowed from the server, which can separate some specific queries from the main server, of course, there is a risk of reading old data. If the primary server shuts down abnormally, one of the slaves will automatically be promoted to the new primary server to continue working. In addition, the replication of MongoDB is beyond the scope of this book.

MongoDB Sharding

MongoDB supports automatic sharding. Sharding is a way to achieve data expansion, relying on data partitioning across servers or clusters. One of the simplest implementations is to put all user data on the server 1 according to the initials AM and then the rest on server 2. Thankfully, MongoDB’s ability to split is far stronger than this. 

Fragmentation is beyond the scope of this book, but you should have the concept of sharding, and you should consider it when your needs grow beyond the use of a single copy set.

Although replication can sometimes improve performance (by isolating long-term queries to the slave or reducing the latency of certain types of queries), its primary purpose is to maintain high availability. 

Sharding is the primary method of extending a MongoDB cluster. Combining replication and sharding for scalability and high availability is a ban.

MongoDB Status

You can  db.stats() query the status of the database. Basically all about the size of the database. You can also query the status of the unicorns collection, such as a collection, which you can enter  db.unicorns.stats(). Basically all about the size of the collection, as well as the index information of the collection.

MongoDB Profiler

You can execute the MongoDB profiler like this:

db.setProfilingLevel(2);

After starting, we can execute a command:

db.unicorns.find({weight: {$gt: 600}});

Then check the profiler:

db.system.profile.find()

The output tells us: when was executed, how many documents were scanned, and how much data was returned.

You have to stop the profiler and only need to call it again  setProfilingLevel , but this time the parameter is  0. Specifying  1 as the first parameter will filter tasks that count more than 100 milliseconds. 100 milliseconds is the default threshold, and you can specify a different threshold time in the second parameter, in milliseconds:

//profile anything that takes
//more than 1 second
db.setProfilingLevel(1, 1000);

MongoDB Backup and Restore

bin There is an executable file in the MongoDB  directory  mongodump . Simple execution  mongodump links to localhost and backs up all your databases to  dump subdirectories. You can use to  mongodump --help see more execution parameters. 

Commonly used parameters are  --db DBNAME backup specified database and --collection COLLECTIONNAME backup specified collection. You can use the  mongorestore executable file, also in the  bin directory, to restore the previous backup. Again,  --db and  --collection you can specify a restored database and/or collection. mongodump And  mongorestore with BSON, this is the native format of MongoDB.

For example, to back up our  learn database guide  backup folder, we need to execute it (execute this command in the console or terminal, not in the mongo shell):

mongodump --db learn --out backup

If we only restore the  unicorns collection, we can do this:

mongorestore --db learn --collection unicorns \
    backup/learn/unicorns.bson

It is worth mentioning that,  mongoexport and  mongoimport is two other executable files for exporting and importing data from JSON/CSV format files. For example, we can export a JSON like this:

mongoexport --db learn --collection unicorns

The CSV format looks like this:

mongoexport --db learn \
    --collection unicorns \
    --csv --fields name,weight,vampires

Attention  mongoexport and  mongoimport not necessarily represent the data correctly. In real backups, you can only use  mongodump and  mongorestore . You can read more about backups from the MongoDB manual.

Summary

In this tutorial, we introduced various commands, tools, and performance details of MongoDB.

Related MongoDB Tutorials For Beginners

What is MongoDB Tutorial For Beginners

MongoDB Basics Tutorial

MongoDB Update Tutorial

MongoDB Query Tutorial

MongoDB Data Modelling Tutorial

MongoDB Scenarios

MongoDB Aggregation Tutorial

Leave a Comment