• 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,

db.mycol.createIndex({“age”:1})
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}
  • 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 :

Categorized in: