AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

cursor.returnKey()(mongoshメソッド)

cursor.returnKey()

重要

mongosh メソッド

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.

MongoDB API ドライバーについては、各言語の「MongoDB ドライバーのドキュメント」を参照してください。

Tip

$meta supports the keyword "indexKey" to return index key metadata if an index is used. The use of { $meta: "indexKey" } is preferred over cursor.returnKey().

カーソルを変更して、ドキュメントではなくインデックス キーを返します。

The cursor.returnKey() has the following form:

cursor.returnKey()
次の値を返します。

The cursor that returnKey() is attached to with a modified result set. This allows for additional cursor modifiers to be chained.

このメソッドは、次の環境でホストされている配置で使用できます。

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

注意

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

クエリがインデックスを使用して読み取り操作を実行しない場合、カーソルは空のドキュメントを返します。

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

コレクションには、デフォルトの_idインデックスに加えて 2 つのインデックスがあります。

{
"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:

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

これにより、以下が返されます。

{ "cuisine" : "Japanese" }
{ "cuisine" : "Japanese" }
{ "cuisine" : "Japanese" }
{ "cuisine" : "Japanese" }
...
このページを評価