参考 > 参考 > 经营者 > 查询修饰符 > $提示
$hint
从v3.2开始不推荐使用
从v3.2开始,shell中$hint不推荐使用该运算符 mongo。在mongo外壳中,cursor.hint()改用。
mongo
cursor.hint()
该$hint运营商强制查询优化器使用特定的索引来满足查询。通过索引名称或文档指定索引。
使用$hint测试查询性能和索引策略。所述mongo外壳提供了一个辅助方法 hint()的$hint操作者。
hint()
请考虑以下操作:
db.users.find().hint( { age: 1 } )
此操作返回users使用age字段索引命名的集合中的所有文档 。
users
age
您还可以使用以下两种形式之一指定提示:
db.users.find()._addSpecial( "$hint", { age : 1 } ) db.users.find( { $query: {}, $hint: { age : 1 } } )
注意
当查询$hint以以下形式指定时:
db.users.find( { $query: {}, $hint: { age : 1 } } )
然后,为了包括该$explain选项,必须将$explain选项添加到文档中,如下所示:
$explain
db.users.find( { $query: {}, $hint: { age : 1 }, $explain: 1 } )
当查询形状存在索引过滤器时,MongoDB将忽略$hint。