What is the use of Index in Mongodb ?

  • MongoDB is one of the leading NoSQL database. It is written in C++ Program.
  • It provides high performance and availability and high scalable.
  • Indexing in MongoDB is used to make query processing more efficient.
  • The MongoDB scan every document in the collection and retrieve only those documents that match the query, where there is no indexing.
  • Indexes are special data structures that stores some information related to the documents such that it becomes easy for MongoDB to find the right data file.
  • Indexes are ordered by the value of the field specified in the index.
Index Creating :
  •  createIndex() is a method that allows user to create an index.
Syntax :

For Example,

[pastacode lang=”markup” manual=”db.mycol.createIndex(%7B%E2%80%9Cage%E2%80%9D%3A1%7D)%0A%7B%0A%E2%80%9CcreatedCollectionAutomatically%E2%80%9D%20%3A%20false%2C%0A%E2%80%9CnumIndexesBefore%E2%80%9D%20%3A%201%2C%0A%E2%80%9CnumIndexesAfter%E2%80%9D%20%3A%202%2C%0A%E2%80%9Cok%E2%80%9D%20%3A%201%0A%7D%20%0A” message=”” highlight=”” provider=”manual”/]
  • In createIndex() method, there are five types of parameters:
    1. background (Boolean)
    2. unique (Boolean)
    3. name (string)
    4. sparse (Boolean)
Index for Drop :
  • dropIndex() is a method that allows users to drop an index.
  • This method can delete only one index at a time.
  • MongoDB provides the dropIndexes() method that takes multiple indexes as its parameters.
Syntax :

Get description of all indexes :
  • It will retrieve all the description of the indexes created within the collection.
Syntax :

Leave a Reply

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

You May Also Like

MongoDB Vs CouchDB

                MongoDB                  CouchDB It Provides faster read speeds. If read speed is critical to your database, MongoDB is faster than CouchDB. No mobile support provided. It can be run…
View Answer

What are the Features of MongoDB ?

It has so many great features while being a NoSQL database. It is unique and attractive. They are making MongoDB widely usable and popular. Features of MongoDB: Schema-less Database Replication…
View Answer