Definição
$getFieldNovidade na versão 5.0.
Returns the value of a specified field from a document. If you don't specify an object,
$getFieldreturns the value of the field from$$CURRENT.You can use
$getFieldto retrieve the value of fields with names that contain periods (.) or start with dollar signs ($).Não confie em nomes de campo com prefixo de dólar de nível raiz que colidem com nomes de campo de metadados internos reservados, pois esses campos podem ser descartados ou inacessíveis em alguns contextos de processamento de query. Para restrições e orientações sobre nomes de campo que contêm cifrões (
$) ou pontos (.), consulte Nomes de campo com pontos e cifrões.Dica
Use
$setFieldpara adicionar ou atualizar campos com nomes que contenham cifrões ($) ou pontos (.).
Sintaxe
$getField tem a seguinte sintaxe:
{ $getField: { field: <String>, input: <Object> } }
Campo | Tipo | Descrição |
|---|---|---|
| String | |
| Objeto |
$getField has the following shorthand syntax for retrieving field values from $$CURRENT:
{ $getField: <String> }
Para esta sintaxe, o argumento é equivalente ao valor de field descrito acima.
Comportamento
Exemplos
Campos de query que contêm pontos (.)
Considere uma coleção inventory com os seguintes documentos:
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 ] } } } ] )
A operação retorna os seguintes resultados:
[ { _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 }, { _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 } ]
Campos de consulta que começam com um cifrão ($)
Considere uma coleção inventory com os seguintes documentos:
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 ] } } } ] )
A operação retorna os seguintes resultados:
[ { _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 }, { _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 } ]
Consultar um campo em um subdocumento
Cria uma collection inventory com os seguintes documentos:
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 } } ] )
A operação seguinte retorna documentos onde o número de $small itens é menor ou igual a 20.
db.inventory.aggregate( [ { $match: { $expr: { $lte: [ { $getField: { field: { $literal: "$small" }, input: "$quantity" } }, 20 ] } } } ] )
Use estes operadores para consultar a coleção:
O operador
$ltelocaliza valores menores ou iguais a 20.$getFieldrequer parâmetrosfieldeinputexplícitos porque o campo$smallfaz parte de um subdocumento.
Saída de exemplo:
[ { _id: 3, item: 'sun dress', 'price.usd': 199.99, quantity: { '$large': 45, '$medium': 40, '$small': 5 } } ]