Performance In MongoDB

We can use index to increase performance. In mongodb we have B-Tree indexes. If we don’t provide index then it will perform collection scan means it scan every document in collection and if index is present then mongodb uses that index to limit the document.

“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “MongoDemo.employee”,
“indexFilterSet” : false,
“parsedQuery” : {
“firstname” : {
“$eq” : “Rudd”
“winningPlan” : {
“stage” : “COLLSCAN”,
“filter” : {
“firstname” : {
“$eq” : “Rudd”
“direction” : “forward”
“rejectedPlans” : [ ]
“serverInfo” : {
“host” : “Sagoba”,
“port” : 27017,
“version” : “4.0.6”,
“gitVersion” : “caa42a1f75a56c7643d0b68d3880444375ec42e3”
“ok” : 1

Performance In MongoDB
Explain Query Without Index Created on Field

If you don’t use index no matter what it will scan all the data. In below it shows the result and only one document is solve. In id have default index .

Performance In MongoDB
Explain Query With Index Created on Field

A explain() method has three verbosity modes.

  • queryPlanner — this is the default mode. At this level, explain provides information about the winning plan, including the index used or if a collection scan is needed.
  • executionStats — this mode includes all the information provided by the queryPlanner plus the statistics. Statistics include details such as the number of documents examined and returned, the execution time in milliseconds, and so on.
  • allPlansExecution — this mode includes all the information provided by the executionStats plus information about the discarded execution plans.

How to create Index ()?


In this on collection we need to use createIndex is function and in this we can pass fieldname as parameter with value 1.

How to find Index ()?


In this getIndexes is the function which is array of indexes and return all element or index element on that collection.

How to drop the index in collection?


It takes parameter index name to drop that index.

ObjectIds in MongoDb

ObjectID in MongoDB is a primary key like present in other databases. This is created by the system but if we want to create we can create it by our self. Ex ObjectId(“5cb962b14ba52ef3a8aafadd”) it is combination of number and letters. Once MongoDB creates ObjectId it is immutable means once mongo created that object then we are not able to modify this. It is unique. It’s bsontype. 12 bytes is the size of that object id in MongoDB. Why this is like number and letter format because as we have a multi server multi-model database and if we use auto increment id then on a different server it might be the chances of repeating or duplication of that id so that’s why it created like this way. As a 12 byte objectID represent this below is the explanation of this

  • a 4-byte value representing the seconds since the Unix epoch,
  • a 5-byte random value, and
  • a 3-byte counter, starting with a random value.

How to create objectID in mongo?


What you will get from ObjectID?

result: ISODate(“2019-04-29T11:20:50Z”)

In this if we created ObjectID by yourself then we can’t get timestamp.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *