文档菜单
文档首页
/
MongoDB 手册
/ / /

getField (聚合)

本页内容

  • 定义
  • 语法
  • 行为
  • 示例
$getField

版本中5.0.

返回文档中指定字段的值。如果您未指定对象,$getField 返回来自$$CURRENT.

您可以使用 $getField 获取包含点(.)或以美元符号($)开头的字段名称的值。

提示

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

$getField 的语法如下

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

您想要返回值的输入对象。

版本 7.2 中已更改字段 接受任何有效的 表达式,该表达式解析为字符串。

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

输入
对象

默认值$$CURRENT

一个包含 字段 的有效 表达式,该字段表示您希望返回值的字段。 输入 必须解析为对象、缺失nullundefined。如果省略,则默认为当前正在处理管道中的文档($$CURRENT)。

$getField 有以下用于从 $$CURRENT 获取字段值的缩写语法:

{
$getField: <String>
}

对于此语法,参数等同于上述描述的 字段 的值。

  • 如果指定的 字段 不在 输入 对象中,或者在您没有指定 输入 对象的情况下在 $$CURRENT 中,$getField 返回 缺失

  • 如果 输入 评估为 缺失undefinednull$getField 返回 null

  • 如果 输入 评估为任何其他非对象、缺失undefinednull 的值,$getField 返回一个错误。

  • $getField 不会隐式遍历对象或数组。例如,$getField 评估 a.b.cfield 值作为顶级字段 a.b.c,而不是嵌套字段 { a: { b: { c: } } }

提示

另请参阅

考虑一个具有以下文档的 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 }

以下操作使用 $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 集合

{ "_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运算符来查找价格大于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 }
}
] )

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

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

使用这些运算符来查询集合

  • $lte运算符查找小于或等于20的值。

  • $getField需要显式的fieldinput参数,因为$small字段是子文档的一部分。

  • $getField使用$literal来评估"$small",因为字段名称中包含美元符号($)。

示例输出

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

返回

函数