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

$getField(式演算子)

$getField

バージョン 5.0 の新機能。

Returns the value of a specified field from a document. If you don't specify an object, $getField returns the value of the field from $$CURRENT.

You can use $getField to retrieve the value of fields with names that contain periods (.) or start with dollar signs ($).

ルートレベルのドルプレフィックスのフィールド名は、一部のクエリプロセシングコンテキストで削除されるか、アクセスできない場合があるため、予約された内部メタデータフィールド名と衝突するものは使用しないでください。ドルフィルドの名前にドル記号$ () やピリオド. () が含まれる場合の制限とガイダンスについては、「 ピリオドとドル記号を含むフィールド名」を参照してください。

Tip

名前にドル記号($)またはピリオド(.)が含まれるフィールドを追加または更新するには、$setField を使用します。

$getField の構文は次のとおりです。

{
$getField: {
field: <String>,
input: <Object>
}
}
フィールド
タイプ
説明

field

文字列

値を返す対象である入力オブジェクト。

バージョン7.2での変更:

field ストリングに解決される有効な 式 を受け入れます。

field がドル記号($)で始まる場合は、フィールド名を $literal 式の中に配置してその値を返します。

input

オブジェクト

デフォルト: $$CURRENT

値を返す対象である field を含む有効な式。input は、オブジェクト missing、null、またはundefined に変換される必要があります。省略した場合、パイプラインで現在処理中のドキュメント($$CURRENT)がデフォルトになります。

$getField has the following shorthand syntax for retrieving field values from $$CURRENT:

{
$getField: <String>
}

この構文では、引数は上記の field の値と同じになります。

  • If the field that you specify is not present in the input object, or in $$CURRENT if you don't specify an input object, $getField returns missing.

  • If input evaluates to missing, undefined, or null, $getField returns null.

  • If input evaluates to anything other than an object, missing, undefined, or null, $getField returns an error.

  • $getField doesn't implicitly traverse objects or arrays. For example, $getField evaluates a field value of a.b.c as a top-level field a.b.c instead of a nested field { a: { b: { c: } } }.

以下のドキュメントを持つinventoryコレクションを検討してください。

db.inventory.insertMany( [
{ _id: 1, item: "sweatshirt", "price.usd": 45.99, qty: 300 },
{ _id: 2, item: "winter coat", "price.usd": 499.99, qty: 200 },
{ _id: 3, item: "sun dress", "price.usd": 199.99, qty: 250 },
{ _id: 4, item: "leather boots", "price.usd": 249.99, qty: 300 },
{ _id: 5, item: "bow tie", "price.usd": 9.99, qty: 180 }
] )

The following operation uses the $getField and $gt operators to find which products have a price.usd greater than 200:

db.inventory.aggregate( [
{
$match:
{ $expr:
{ $gt: [ { $getField: "price.usd" }, 200 ] }
}
}
] )

この操作は次の結果を返します。

[
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 }
]

以下のドキュメントを持つinventoryコレクションを検討してください。

db.inventory.insertMany( [
{ _id: 1, item: "sweatshirt", "$price": 45.99, qty: 300 },
{ _id: 2, item: "winter coat", "$price": 499.99, qty: 200 },
{ _id: 3, item: "sun dress", "$price": 199.99, qty: 250 },
{ _id: 4, item: "leather boots", "$price": 249.99, qty: 300 },
{ _id: 5, item: "bow tie", "$price": 9.99, qty: 180 }
] )

The following operation uses the $getField, $gt, and $literal operators to find which products have a $price greater than 200:

db.inventory.aggregate( [
{
$match:
{ $expr:
{ $gt: [ { $getField: {$literal: "$price" } }, 200 ] }
}
}
] )

この操作は次の結果を返します。

[
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }
]

次のドキュメントを使用して inventory コレクションを作成します。

db.inventory.insertMany( [
{ _id: 1, item: "sweatshirt", "price.usd": 45.99,
quantity: { "$large": 50, "$medium": 50, "$small": 25 }
},
{ _id: 2, item: "winter coat", "price.usd": 499.99,
quantity: { "$large": 35, "$medium": 35, "$small": 35 }
},
{ _id: 3, item: "sun dress", "price.usd": 199.99,
quantity: { "$large": 45, "$medium": 40, "$small": 5 }
},
{ _id: 4, item: "leather boots", "price.usd": 249.99,
quantity: { "$large": 20, "$medium": 30, "$small": 40 }
},
{ _id: 5, item: "bow tie", "price.usd": 9.99,
quantity: { "$large": 0, "$medium": 10, "$small": 75 }
}
] )

次の操作は、$small アイテムの数が 20 以下であるドキュメントを返します。

db.inventory.aggregate( [
{ $match:
{ $expr:
{ $lte:
[
{ $getField:
{ field: { $literal: "$small" },
input: "$quantity"
}
},
20
]
}
}
}
] )

次の演算子を使用して、コレクションをクエリします。

  • $lte 演算子は、20 以下の値を検索します。

  • $getField には、明示的な field パラメーターと input パラメーターが必要です。 $small フィールドはサブドキュメントの一部であるためです。

  • $getField uses $literal to evaluate "$small", because the field name has a dollar sign ($) in it.

出力例:

[
{
_id: 3,
item: 'sun dress',
'price.usd': 199.99,
quantity: { '$large': 45, '$medium': 40, '$small': 5 }
}
]
このページを評価