Indexes in MongoDB?

0
(0)

Indexes in MongoDB?

Q1: What are indexes in MongoDB?
In MongoDB, indexes provide high-performance read operations for frequently used queries.
indexes special structures in MongoDB, which stores a small portion of the data set in an easy to traverse form. the index stores the value of a specific field or set of fields.
— Used to execute query efficiently.
Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a
collection, to select those documents that match the query statement.
— By default, which index is created by MongoDB for every collection?
— By default, the_id collection is created for every collection by MongoDB.
 

 

Create Index

To create an index, use createIndex() method of MongoDB.
db.COLLECTION_NAME.createIndex({KEY:1})

>db.mycol.createIndex({"title":1})

Can create index on mutliple fields
db.mycol.createIndex({"title":1,"ISBN":-1})

 

 

Drop an Index

Drop a particular index
db.COLLECTION_NAME.dropIndex({KEY:1})
db.mycol.dropIndex({"title":1})

 

Drop multiple indexes
db.mycol.dropIndexes({"title":1,"description":-1})
{ "nIndexesWas" : 2, "ok" : 1 }

 

 

To check an Index
db.COLLECTION_NAME.getIndexes()

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Be the first to comment

Leave a Reply

Your email address will not be published.


*


10 + fourteen =