Database Manual / Indexes

Indexing Reference

Indexing Methods in mongosh

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

createIndexes

Builds one or more indexes for a collection.

dropIndexes

Removes indexes from a collection.

compact

Defragments a collection and rebuilds the indexes.

reIndex

Rebuilds all indexes on a collection.

validate

Internal command that scans for a collection's data and indexes for correctness.

setIndexCommitQuorum

Changes the minimum number of data-bearing members (i.e commit quorum), including the primary, that must vote to commit an in-progress index build before the primary marks those indexes as ready.

Geospatial Query Selectors

NameDescription

$geoWithin

Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin.

$geoIntersects

Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.

$near

Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.

$nearSphere

Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.