Indexing Reference

Indexing Methods in the mongo Shell

NameDescription
db.collection.createIndex()Builds an index on a collection.
db.collection.dropIndex()Removes a specified index on a collection.
db.collection.dropIndexes()Removes all indexes on a collection.
db.collection.getIndexes()Returns an array of documents that describe the existing indexes on a collection.
db.collection.reIndex()Rebuilds all existing indexes on a collection.
db.collection.totalIndexSize()Reports the total size used by the indexes on a collection. Provides a wrapper around the totalIndexSize field of the collStats output.
cursor.explain()Reports on the query execution plan for a cursor.
cursor.hint()Forces MongoDB to use a specific index for a query.
cursor.max()Specifies an exclusive upper index bound for a cursor. For use with cursor.hint()
cursor.min()Specifies an inclusive lower index bound for a cursor. For use with cursor.hint()

Indexing Database Commands

NameDescription
createIndexesBuilds one or more indexes for a collection.
dropIndexesRemoves indexes from a collection.
compactDefragments a collection and rebuilds the indexes.
reIndexRebuilds all indexes on a collection.
validateInternal command that scans for a collection’s data and indexes for correctness.
geoSearchPerforms a geospatial query that uses MongoDB’s haystack index functionality.
checkShardingIndexInternal command that validates index on shard key.

Geospatial Query Selectors

NameDescription
$geoWithinSelects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support$geoWithin.
$geoIntersectsSelects geometries that intersect with a GeoJSON geometry.The 2dsphere index supports$geoIntersects.
$nearReturns geospatial objects in proximity to a point.Requires a geospatial index. The 2dsphere and 2d indexes support$near.
$nearSphereReturns geospatial objects in proximity to a point on a sphere.Requires a geospatial index. The 2dsphere and 2d indexes support$nearSphere.

Indexing Query Modifiers

NameDescription
$explainForces MongoDB to report on query execution plans. See explain().
$hintForces MongoDB to use a specific index. See hint()
$maxSpecifies an exclusive upper limit for the index to use in a query. See max().
$minSpecifies an inclusive lower limit for the index to use in a query. See min().
$returnKeyForces the cursor to only return fields included in the index.