cursor.hint()

On this page本页内容

Definition定义

cursor.hint(index)

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的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描述
index string or document

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

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}来执行反向集合扫描。

Behavior行为

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