Navigation
This version of the documentation is archived and no longer supported.

$hint

$hint

The $hint operator forces the query optimizer to use a specific index to fulfill the query. Specify the index either by the index name or by the index specification document. See Index Specification Documents for information on index specification documents.

Use $hint for testing query performance and indexing strategies. The mongo shell provides a helper method hint() for the $hint operator.

Consider the following operation:

db.users.find().hint( { age: 1 } )

This operation returns all documents in the collection named users using the index on the age field.

You can also specify a hint using either of the following forms:

db.users.find()._addSpecial( "$hint", { age : 1 } )
db.users.find( { $query: {}, $hint: { age : 1 } } )

Note

To combine $explain and $hint operations, use the following form:

db.users.find( { $query: {}, $hint: { age : 1 } } )

You must add the $explain option to the document, as in the following:

db.users.find( { $query: {}, $hint: { age : 1 }, $explain: 1 } )
←   $explain $max  →