$hint
MongoDB 5.0 is end of life as of October 2024. This version of the documentation is no longer
supported. To upgrade your 5.0 deployment, see the MongoDB 6.0 upgrade procedures.
$hint
Note
Deprecated since v3.2
Starting in v3.2, the
$hint
operator is deprecated inmongosh
. Inmongosh
, usecursor.hint()
instead.The
$hint
operator may still be available for specific drivers.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 document.Use
$hint
for testing query performance and indexing strategies.mongosh
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 named
users
using the index on theage
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 an index filter exists for the query shape, MongoDB ignores the
$hint
.