Database Manual / Reference / mongosh Methods / Cursors

cursor.hint() (mongosh method方法)

Definition定义

cursor.hint(index)

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.本页记录了一种mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

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()方法有以下参数:

Parameter参数Type类型Description描述
indexstring or document字符串或文档

The index to "hint" or force MongoDB to use when performing the query. Specify the index either by the index name or by the index specification document.“提示”或强制MongoDB在执行查询时使用的索引。通过索引名称或索引规范文档指定索引。

You can also specify { $natural : 1 } to force the query to perform a forwards collection scan, or { $natural : -1 } for a reverse collection scan.您还可以指定{ $natural : 1 }强制查询执行正向集合扫描,或指定{ $natural : -1 }强制执行反向集合扫描。

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Behavior行为

  • When an index filter exists for the query shape, MongoDB ignores the hint().当查询形状存在索引筛选器时,MongoDB会忽略hint()
  • If a query includes a $text expression, you cannot use hint() to specify which index to use for the query.如果查询包含$text表达式,则不能使用hint()指定查询使用的索引。
  • If you use hint() on a hidden index or an index that doesn't exist, the operation returns an error.如果对隐藏索引或不存在的索引使用hint(),则操作将返回错误。
  • On a time series collections, you can only specify hints using the index name, not the index key pattern.时间序列集合上,只能使用索引名称指定提示,而不能使用索引键模式。

$natural

$natural

Use $natural in conjunction with cursor.hint() to perform a collection scan to return documents in natural order.$naturalcursor.hint()结合使用,执行集合扫描,以自然顺序返回文档。

For usage, see Force Collection Scans.有关用法,请参阅强制采集扫描

Note

Prior to MongoDB 7.0, $natural accepts incorrect type values, such as 0, NaN, and -0.01. 在MongoDB 7.0之前,$natural接受不正确的类型值,如0NaN-0.01After MongoDB 7.0, if you pass any value other than 1 and -1 to $natural, MongoDB returns an error.在MongoDB 7.0之后,如果将1-1以外的任何值传递给$natural,MongoDB将返回错误。

Examples示例

Specify an Index指定索引

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" )

Force Collection Scans强制采集扫描

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 } )