$hint
Deprecated since v3.2
Starting in v3.2, the $hint
operator is deprecated in themongo
shell. In the mongo
shell,use cursor.hint()
instead.
The $hint
operator forces the query optimizer to use a specific index tofulfill the query. Specify the index either by the index name or bydocument.
Use $hint
for testing query performance and indexingstrategies. The mongo
shell provides a helper methodhint()
for the $hint
operator.
Consider the following operation:
- db.users.find().hint( { age: 1 } )
This operation returns all documents in the collection namedusers
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
When the query specifies the $hint
in the followingform:
- db.users.find( { $query: {}, $hint: { age : 1 } } )
Then, in order to include the $explain
option, youmust add the $explain
option to the document, as inthe following:
- db.users.find( { $query: {}, $hint: { age : 1 }, $explain: 1 } )
When an index filter exists for the queryshape, MongoDB ignores the $hint
.