정의
구문
$getField 의 구문은 다음과 같습니다:
{   $getField: {     field: <String>,     input: <Object>   } } 
| 필드 | 유형 | 설명 | 
|---|---|---|
| 
 | 문자열 | |
| 
 | 객체 | 
$getField에는 $$CURRENT에서 필드 값을 검색하기 위한 다음과 같은 단축 구문이 있습니다.
{   $getField: <String> } 
이 구문의 경우 인수는 위에 설명된 field 값과 동일합니다.
행동
- field가 문자열 상수가 아닌 것으로 확인되면- $getField는 오류를 반환합니다.
- 지정한 - field가- input객체에 없거나,- input객체를 지정하지 않은 경우- $$CURRENT에 없으면- $getField는- missing을 반환합니다.
- input이- missing,- undefined또는- null로 평가되면- $getField는- null을 반환합니다.
- input이 객체,- missing,- undefined또는- null이외의 것으로 평가되면- $getField는 오류를 반환합니다.
- $getField는 객체 또는 배열을 암시적으로 순회하지 않습니다. 예를 들어,- $getField는- a.b.c의- field값을 중첩된 필드- { a: { b: { c: } } }대신 최상위 필드- 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 } ] ) 
다음 작업은 $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 collection을 생성합니다.
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             ]          }       }    } ] ) 
이러한 연산자를 사용하여 collection을 쿼리합니다:
- $lte연산자는 20보다 작거나 같은 값을 찾습니다.
- $small필드가 하위 문서의 일부이므로- $getField에는 명시적인- field및- input매개 변수가 필요합니다.
- $getField는 필드 이름에 달러 기호(- $)가 있기 때문에- $literal을 사용하여 '- $small'을 평가합니다.
출력 예시:
[   {     _id: 3,     item: 'sun dress',     'price.usd': 199.99,     quantity: { '$large': 45, '$medium': 40, '$small': 5 }   } ]