Introduction to indexing
Indexes support the efficient execution of queries in MongoDB. 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. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.
Indexes are special data structures that store a small portion of the collection's data set in an easy to traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field. The ordering of the index entries supports efficient equality matches and range-based query operations. In addition, MongoDB can return sorted results by using the ordering in the index.
MongoDB supports indexes
– At the collection level
– Similar to indexes on RDBMS
Can be used for
– More efficient filtering
– More efficient sorting
– Index-only queries (covering index)
Types of Indexes
→ Default _id Index
MongoDB creates a unique index on the _id field during the creation of a collection.
The _id index prevents clients from inserting two documents with the same value for the
_id field.
You cannot drop this index on the _id field.
→ Create an Index
To create an index, use
db.collection.createIndex()
db.collection.createIndex( <key and index type specification>, <options> ) |
The db.collection.createIndex() method only creates an index if an index of the same
specification does not already exist.
MongoDB indexes use a B-tree data structure.
MongoDB provides several different index types to support specific types of data and
queries.
→ Single Field
In addition to the MongoDB-defined _id index, MongoDB supports the creation of user-defined
ascending/descending indexes on a single field of a document.
The following example creates an ascending index on the field orderDate.
db.collection.createIndex( { orderDate: 1 } ) |
→ Compound Index
MongoDB also supports user-defined indexes on multiple fields, i.e. compound indexes.
The order of fields listed in a compound index has significance. For instance, if a compound index consists of { userid: 1, score: -1 }, the index sorts first by userid and then, within each userid value, sorts by score.
The following example creates a compound index on the orderDate field (in ascending order) and the zip code field (in descending order.)
db.collection.createIndex( { orderDate: 1, zipcode: -1 } ) |
→ Multikey Index
MongoDB uses multikey indexes to index the content stored in arrays.
If you index a field that holds an array value, MongoDB creates separate index entries for every element of the array.
These multikey indexes allow queries to select documents that contain arrays by matching on elements or elements of the arrays.
MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value; you do not need to explicitly specify the multikey type.
→ Index Use
Indexes can improve the efficiency of reading operations.
Covered Queries
When the query criteria and the projection of a query includes only the indexed fields,
MongoDB will return results directly from the index without scanning any documents or
bringing documents into memory. These covered queries can be very efficient.
Index Intersection(New in version 2.6.)
MongoDB can use the intersection of indexes to fulfil queries.
For queries that specify the compound query conditions, if one index can fulfil a part of a query condition, and another index can fulfil another part of the query condition, then MongoDB can
use the intersection of the two indexes to fulfil the query.
To illustrate index intersection, consider collection orders that have the following
indexes:
{ qty: 1 }
{ item: 1 }
MongoDB can use the intersection of the two
indexes to support the following query:
db.orders.find( { item: "abc123", qty: { $gt: 15} } ) |
→ Remove Indexes
You can use the following methods to remove indexes:
db.collection.dropIndex() method
db.accounts.dropIndex( { "tax-id": 1 } ) |
The above operation removes an ascending index on the item field in the items collection.
Db.collection.drop indexes() |
To remove all indexes barring the _id index from a collection, use the operation above.
→ Modify Indexes
To modify an index, first, drop the index and then recreate it.
Drop Index: Execute the query given below to return a document showing the operation status.
db.orders.dropIndex({ "cust_id" : 1, "ord_date" :-1, "items" : 1 }) |
Recreate the Index: Execute the query given below to return a document showing the status of the results.
db.orders.createIndex({ "cust_id" : 1, "ord_date" : -1, "items" : -1 }) |
→ Rebuild Indexes
In addition to modifying indexes, you can also rebuild them.
To rebuild all indexes of a collection, use the db.collection.reIndex() method.
This will drop all indexes including _id and rebuild all indexes in a single operation.
0 Comments