定義
$getField5.0バージョン の新機能。:
ドキュメントから指定したフィールドの値を返します。 オブジェクトを指定しない場合、
$getFieldは$$CURRENTからフィールドの値を返します。$getFieldを使用すると、名前にピリオド(.)が含まれるフィールドやドル記号($)で始まるフィールドの値を取得できます。予約済みの内部メタデータフィールド名と競合するルートレベルのドル記号プレフィックスがついたフィールド名には依存しないでください。これらのフィールドは一部のクエリ処理コンテキストでは削除されたりアクセスできなくなったりする可能性があるためです。ドル記号( )またはピリオド( )を含むフィールド名に関する制限とガイダンスについては、「 ピリオドとドル記号を含むフィールド名
$.」を参照してください。Tip
名前にドル記号(
$)またはピリオド(.)が含まれるフィールドを追加または更新するには、$setFieldを使用します。
構文
$getField の構文は次のとおりです。
{ $getField: { field: <String>, input: <Object> } }
フィールド | タイプ | 説明 |
|---|---|---|
| 文字列 | 値を返す対象である入力オブジェクト。 7.2バージョン での変更。
|
| オブジェクト |
$getFieldには、 $$CURRENTからフィールド値を取得するための次の短縮構文があります。
{ $getField: <String> }
この構文では、引数は上記の field の値と同じになります。
動作
例
ピリオド(. )を含むクエリ フィールド
以下のドキュメントを持つ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 } ] )
次の操作では、 $getField演算子と$gt演算子を使用して、 price.usdが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 } ] )
次の操作では、 $getField 、 $gt 、および$literal演算子を使用して、 $priceが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には明示的なfieldinput$smallパラメーターと パラメーターが必要です。 フィールドはサブドキュメントの一部であるためです。
出力例:
[ { _id: 3, item: 'sun dress', 'price.usd': 199.99, quantity: { '$large': 45, '$medium': 40, '$small': 5 } } ]