$getField (aggregation)
On this page本页内容
Definition定义
$getField
New in version 5.0.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
.$getField
将返回$$CURRENT
中字段的值。You can use您可以使用$getField
to retrieve the value of fields with names that contain periods (.
) or start with dollar signs ($
).$getField
来检索名称包含句点(.
)或以美元符号($
)开头的字段的值。
Syntax语法
$getField
has the following syntax:具有以下语法:
{
$getField: {
field: <String>,
input: <Object>
}
}
field | String | input object for which you want to return a value. input 对象中的字段。field can be any valid expression that resolves to a string constant.field 可以是解析为字符串常量的任何有效表达式。field begins with a dollar sign ($ ), place the field name inside of a $literal expression to return its value. field 以美元符号($ )开头,则将字段名放在$literal 表达式中以返回其值。 |
input | Object | Default: $$CURRENT field for which you want to return a value. input must resolve to an object, missing , null , or undefined . missing 、null 或undefined 。$$CURRENT ). $$CURRENT )。 |
$getField
has the following shorthand syntax for retrieving field values from 具有以下简写语法,用于从$$CURRENT
:$$CURRENT
中检索字段值:
{
$getField: <String>
}
For this syntax, the argument is equivalent to the value of 对于此语法,自变量等效于上述field
described above.field
的值。
Behavior行为
If若field
resolves to anything other than a string constant,$getField
returns an error.field
解析为字符串常量以外的任何值,$getField
将返回一个错误。If the如果指定的field
that you specify is not present in theinput
object, or in$$CURRENT
if you don't specify aninput
object,$getField
returnsmissing
.field
不存在于input
对象中,或者如果未指定输入对象,则在$$CURRENT
中,$getField
将返回missing
。If如果input
evaluates tomissing
,undefined
, ornull
,$getField
returnsnull
.input
的计算结果为missing
、undefined
或null
,$getField
将返回null
。If如果input
evaluates to anything other than an object,missing
,undefined
, ornull
,$getField
returns an error.input
的计算结果不是对象、missing
、undefined
或为null
,$getField
将返回一个错误。$getField
doesn't implicitly traverse objects or arrays. For example,不隐式遍历对象或数组。例如,$getField
evaluates afield
value ofa.b.c
as a top-level fielda.b.c
instead of a nested field{ a: { b: { c: } } }
.$getField
将field
值a.b.c
计算为顶级字段a.b.c
,而不是嵌套字段{ a: { b: { c: } } }
。
See also: 另请参阅:
Examples实例
Query Fields that Contain Periods (.
)查询包含点号(.
)的字段
.
)Consider an 考虑一个包含以下文档的inventory
collection with the following documents:inventory
集合:
{ "_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
:$getField
和$gt
运算符查找price.usd
大于200
的产品:
db.inventory.aggregate( [
{
$match:
{ $expr:
{ $gt: [ { $getField: "price.usd" }, 200 ] }
}
}
] )
The operation returns the following results:该操作返回以下结果:
[
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 }
]
Query Fields that Start with a Dollar Sign ($
)以美元符号($
)开头的查询字段
$
)Consider an 考虑一个包含以下文档的inventory
collection with the following documents:inventory
集合:
{ "_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
:$getField
、$gt
和$literal
运算符来查找哪些产品的$price
大于200
:
db.inventory.aggregate( [
{
$match:
{ $expr:
{ $gt: [ { $getField: {$literal: "$price" } }, 200 ] }
}
}
] )
The operation returns the following results:该操作返回以下结果:
[
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }
]
Query a Field in a Sub-document查询子文档中的字段
Create an 使用以下文档创建inventory
collection with the following documents: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 }
}
] )
The following operation returns documents where the number of 以下操作返回$small
items is less than or equal to 20
.$small
项目数小于或等于20
的文档。
db.inventory.aggregate( [
{ $match:
{ $expr:
{ $lte:
[
{ $getField:
{ field: { $literal: "$small" },
input: "$quantity"
}
},
20
]
}
}
}
] )
Use these operators to query the collection:使用以下运算符查询集合:
The$lte
operator finds values less than or equal to 20.$lte
运算符查找小于或等于20的值。$getField
requires explicit需要显式field
andinput
parameters because the$small
field is part of a sub-document.field
和input
参数,因为$small
字段是子文档的一部分。$getField
uses使用$literal
to evaluate "$small
", because the field name has a dollar sign ($
) in it.$literal
计算“$small
”,因为字段名中有一个美元符号($
)。
Example output:示例输出:
[
{
_id: 3,
item: 'sun dress',
'price.usd': 199.99,
quantity: { '$large': 45, '$medium': 40, '$small': 5 }
}
]