Database Manual / Reference / mongosh Methods / Cursors

cursor.returnKey() (mongosh method方法)

Definition定义

cursor.returnKey()

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驱动程序文档

Tip

$meta supports the keyword "indexKey" to return index key metadata if an index is used. 支持键"indexKey"在使用索引时返回索引键元数据。The use of { $meta: "indexKey" } is preferred over cursor.returnKey().使用{ $meta: "indexKey" }cursor.returnKey()更可取。

Modifies the cursor to return index keys rather than the documents.修改游标以返回索引键而不是文档。

The cursor.returnKey() has the following form:cursorreturnKey()具有以下形式:

cursor.returnKey()
Returns:返回The cursor that returnKey() is attached to with a modified result set. This allows for additional cursor modifiers to be chained.返回returnKey()游标附加了修改后的结果集。这允许链接其他游标修改器。

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行为

If the query does not use an index to perform the read operation, the cursor returns empty documents.如果查询不使用索引来执行读取操作,则游标将返回空文档。

Example示例

The restaurants collection contains documents with the following schema:restaurants集合包含具有以下模式的文档:

db.restaurants.insertOne(
{
_id: ObjectId("564f3a35b385149fc7e3fab9"),
address: {
building: "2780",
coord: [
-73.98241999999999,
40.579505
],
street: "Stillwell Avenue",
zipcode: "11224"
},
borough: "Brooklyn",
cuisine: "American ",
grades: [
{
date: ISODate("2014-06-10T00:00:00Z"),
grade: "A",
score: 5
},
{
date: ISODate("2013-06-05T00:00:00Z"),
grade: "A",
score: 7
}
],
name: "Riviera Caterer",
restaurant_id: "40356018"
}
)

The collection has two indexes in addition to the default _id index:除了默认的_id索引外,该集合还有两个索引:

{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "guidebook.restaurant"
},
{
"v" : 1,
"key" : {
"cuisine" : 1
},
"name" : "cuisine_1",
"ns" : "guidebook.restaurant"
},
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "name_text",
"ns" : "guidebook.restaurant",
"weights" : {
"name" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}

The following code uses the cursor.returnKey() method to return only the indexed fields used for executing the query:以下代码使用cursor.returnKey()方法仅返回用于执行查询的索引字段:

var csr = db.restaurant.find( { "cuisine" : "Japanese" } )
csr.returnKey()

This returns the following:这将返回以下内容:

{ "cuisine" : "Japanese" }
{ "cuisine" : "Japanese" }
{ "cuisine" : "Japanese" }
{ "cuisine" : "Japanese" }
...