정의
$getField버전 5.0에 추가 되었습니다.:
문서에서 지정된 필드의 값을 반환합니다. 객체를 지정하지 않으면
$getField은(는)$$CURRENT의 필드 값을 반환합니다.$getField를 사용하여 마침표(.)를 포함하거나 달러 기호($)로 시작하는 이름을 가진 필드의 값을 조회할 수 있습니다.예약된 내부 메타데이터 필드 이름과 충돌하는 루트 수준 달러 접두사가 붙은 필드 이름에 의존하지 마세요. 해당 필드는 일부 쿼리 처리 컨텍스트에서 삭제되거나 액세스할 수 없을 수 있습니다. 달러 기호()
$또는 마침표()가 포함된 필드 이름에 대한 제한 사항 및 지침 마침표.및 달러 기호가 있는 필드이름을 참조하세요.팁
$setField을(를) 사용하여 달러 기호($) 또는 마침표(.)가 포함된 이름의 필드를 추가하거나 업데이트합니다.
구문
$getField 의 구문은 다음과 같습니다:
{ $getField: { field: <String>, input: <Object> } }
필드 | 유형 | 설명 |
|---|---|---|
| 문자열 | |
| 객체 |
$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 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을 쿼리합니다:
출력 예시:
[ { _id: 3, item: 'sun dress', 'price.usd': 199.99, quantity: { '$large': 45, '$medium': 40, '$small': 5 } } ]