$hint

$hint

Deprecated since v3.2

Starting in v3.2, the $hint operator is deprecated in the mongo shell. 从v3.2开始,$hint运算符在mongo shell中不受欢迎。In the mongo shell, use cursor.hint() instead.mongo shell中,请改用cursor.hint()

The $hint operator forces the query optimizer to use a specific index to fulfill the query. $hint运算符强制查询优化器使用特定的索引来完成查询。Specify the index either by the index name or by document.通过索引名称或文档指定索引。

Use $hint for testing query performance and indexing strategies. 使用$hint测试查询性能和索引策略。The mongo shell provides a helper method hint() for the $hint operator.mongo shell为$hint运算符提供了一个辅助器方法hint()

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.此操作使用age字段上的索引返回集合中名为users的所有文档。

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 following form:当查询以以下形式指定$hint时:

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

Then, in order to include the $explain option, you must add the $explain option to the document, as in the following:然后,为了包含$explain选项,必须将$explain选项添加到文档中,如下所示:

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

When an index filter exists for the query shape, MongoDB ignores the $hint.当查询形状存在索引筛选器时,MongoDB会忽略$hint