Thursday, 3 August 2023

Indexes in MongoDB

 In MongoDB also have indexes same as other database but what kind of indexes and functionality below we are discuss.

Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indexes can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records. In mongodb documents is uniquely defined with _id columns you can see when executing the db.collection.find() but application team some time gives the object id as of own understanding. If you getting some performance issue all type of issue written in mongod.log file as per select the profiling late to dicuss about these topics Mongod.log / Profiling.

 

Create Index

Syntax: ensureIndex()    <——– Now 3.2 onword its depricated 

Syntax: createIndex()

> db.student.createIndex({“rollno”:1})

> db.student.reIndex()           <———- Index rebuild for defrag the indexes

> db.student.dropIndex( { “rollno”: 1 } )   <——- drop index from collection

> db.student.getIndexes();     <——– Get the all list of indexes which is available on student collection

 

Below Using Test Database in mongoDB and below the show you complete list of indexes available in TEST database only.

> use TEST 

> db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
print(“Indexes for ” + collection + “:”);
printjson(indexes);
});

For the details about indexes and type of indexes available in mongoDB : Click Here

No comments: