Database Manual / Reference / Query Language / Expressions

$getField (expression operator)(表达式运算符)

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检索名称包含句点(.)或以美元符号($)开头的字段的值。

Tip

Use $setField to add or update fields with names that contain dollar signs ($) or periods (.).使用$setField添加或更新名称包含美元符号($)或句点(.)的字段。

Syntax语法

$getField has the following syntax:具有以下语法:

{
$getField: {
field: <String>,
input: <Object>
}
}
Field字段Type类型Description描述
fieldString字符串

Input object for which you want to return a value.要为其返回值的输入对象。

Changed in version 7.2.在版本7.2中的更改。 field accepts any valid expression that resolves to a string.field接受解析为字符串的任何有效表达式

If field begins with a dollar sign ($), place the field name inside of a $literal or $toString expression to return its value.如果field以美元符号($)开头,请将字段名放在$literal$toString表达式中以返回其值。

inputObject对象

Default: 默认值:$$CURRENT

A valid expression that contains the field for which you want to return a value. 包含要为其返回值的field的有效表达式input must resolve to an object, missing, null, or undefined. 输入必须解析为对象、缺失、nullundefinedIf omitted, defaults to the document currently being processed in the pipeline ($$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 the field that you specify is not present in the input object, or in $$CURRENT if you don't specify an input object, $getField returns missing.如果您指定的fieldinput对象中不存在,或者在$$CURRENT中不存在(如果您没有指定input对象),$getField将返回缺失。
  • If input evaluates to missing, undefined, or null, $getField returns null.如果input计算结果为缺失、undefinednull$getField将返回null
  • If input evaluates to anything other than an object, missing, undefined, or null, $getField returns an error.如果input的结果不是对象、缺失、undefined义或null$getField将返回错误。
  • $getField doesn't implicitly traverse objects or arrays. For example, $getField evaluates a field value of a.b.c as a top-level field a.b.c instead of a nested field { a: { b: { c: } } }.$getField不会隐式遍历对象或数组。例如,$getFielda.b.cfield值计算为顶级字段a.b.c,而不是嵌套字段{ a: { b: { c: } } }

Examples示例

Query Fields that Contain Periods (.)包含句点(.)的查询字段

Consider an inventory collection with the following documents:考虑使用以下文件进行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 }
] )

The following operation uses the $getField and $gt operators to find which products have a price.usd greater than 200:以下操作使用$getField$gt运算符来查找哪些产品的价格大于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集合:

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:以下操作使用$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 and input parameters because the $small field is part of a sub-document.$getField需要显式的fieldinput参数,因为$small字段是子文档的一部分。
  • $getField uses $literal to evaluate "$small", because the field name has a dollar sign ($) in it.$getField使用$literal来计算“$small”,因为字段名中有一个美元符号($)。

Example output:示例输出:

[
{
_id: 3,
item: 'sun dress',
'price.usd': 199.99,
quantity: { '$large': 45, '$medium': 40, '$small': 5 }
}
]