对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$getField(表达式操作符)

$getField

版本 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.

You can use $getField to retrieve the value of fields with names that contain periods (.) or start with dollar signs ($).

请勿依赖与保留的内部元数据字段名称冲突的根级前缀为美元的字段名称,因为在某些查询处理上下文中,这些字段可能会被删除或无法访问。有关包含美元符 ($)或句号 (.)的字段名称的限制和指导,请参阅带有句号和美元符的字段名称。

提示

使用 $setField 添加名称中包含美元符号 ($) 或句点 (.) 的字段。

$getField 通过以下语法实现:

{
$getField: {
field: <String>,
input: <Object>
}
}
字段
类型
说明

field

字符串

要返回值的 input 对象中的字段。field 可以是解析为字符串常量的任何有效表达式

如果 field 以美元符号 ($) 开头,则将字段名称放在 $literal 表达式中以返回其值。

input

对象

默认值$$CURRENT

一个有效的表达式,包含要返回值的 fieldinput 必须解析为对象、missingnullundefined。如果省略,则默认为管道中当前正在处理的文档 ($$CURRENT)。

$getField has the following shorthand syntax for retrieving field values from $$CURRENT:

{
$getField: <String>
}

对于此语法,参数等效于上述 field 的值。

  • If field resolves to anything other than a string constant, $getField returns an error.

  • 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.

  • If input evaluates to missing, undefined, or null, $getField returns null.

  • If input evaluates to anything other than an object, missing, undefined, or null, $getField returns an error.

  • $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: } } }.

考虑包含以下文档的 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:

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 }
] )

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 ] }
}
}
] )

操作返回以下结果:

[
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }
]

使用以下文档创建 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 }
}
] )

以下操作将返回 $small 项的数量小于或等于 20 的文档。

db.inventory.aggregate( [
{ $match:
{ $expr:
{ $lte:
[
{ $getField:
{ field: { $literal: "$small" },
input: "$quantity"
}
},
20
]
}
}
}
] )

使用这些操作符来查询集合:

  • $lte 操作符查找小于等于 20 的值。

  • $getField requires explicit field and input parameters because the $small field is part of a sub-document.

  • $getField uses $literal to evaluate "$small", because the field name has a dollar sign ($) in it.

示例输出:

[
{
_id: 3,
item: 'sun dress',
'price.usd': 199.99,
quantity: { '$large': 45, '$medium': 40, '$small': 5 }
}
]
给本页内容打分