On this page本页内容
cursor.hint(index)
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
Call this method on a query to override MongoDB's default index selection and query optimization process. 对查询调用此方法以覆盖MongoDB的默认索引选择和查询优化过程。Use 使用db.collection.getIndexes()
to return the list of current indexes on a collection.db.collection.getIndexes()
返回集合上的当前索引列表。
The cursor.hint()
method has the following parameter:cursor.hint()
方法具有以下参数:
index | string or document |
|
hint()
.hint()
。hint()
if the query includes a $text
query expression.$text
查询表达式,则不能使用hint()
。hint()
on a hidden index.hint()
。The following example returns all documents in the collection named 以下示例使用users
using the index on the age
field.age
字段上的索引返回集合中名为users
的所有文档。
db.users.find().hint( { age: 1 } )
You can also specify the index using the index name:还可以使用索引名称指定索引:
db.users.find().hint( "age_1" )
You can specify 您可以指定{ $natural : 1 }
to force the query to perform a forwards collection scan:{$natural:1}
以强制查询执行正向集合扫描:
db.users.find().hint( { $natural : 1 } )
You can also specify 还可以指定{ $natural : -1 }
to force the query to perform a reverse collection scan:{$natural:-1}
强制查询执行反向集合扫描:
db.users.find().hint( { $natural : -1 } )