On this page本页内容
cursor.
returnKey
()¶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驱动程序文档。
Tip
Starting in MongoDB 4.4, 从MongoDB 4.4开始,$meta
supports the keyword "indexKey"
to return index key metadata if an index is used. $meta
支持关键字"indexKey"
,以便在使用索引时返回索引键元数据。The use of 与{ $meta: "indexKey" }
is preferred over cursor.returnKey()
.cursor.returnKey()
相比,更倾向于使用{ $meta: "indexKey" }
。
Modifies the cursor to return index keys rather than the documents.修改游标以返回索引键而不是文档。
The cursor.returnKey()
has the following form:cursor.returnKey()
具有以下形式:
returnKey() is attached to with a modified result set. returnKey() 所附着的游标带有修改后的结果集。 |
If the query does not use an index to perform the read operation, the cursor returns empty documents.如果查询不使用索引来执行读取操作,游标将返回空文档。
The restaurants
collection contains documents with the following schema:restaurants
集合包含具有以下架构的文档:
The collection has two indexes in addition to the default 除了默认的_id
index:_id
索引外,集合还有两个索引:
The following code uses the 以下代码使用cursor.returnKey()
method to return only the indexed fields used for executing the query:cursor.returnKey()
方法仅返回用于执行查询的索引字段:
This returns the following:这将返回以下内容: