文章目录
- 语法
- 使用
- 举例
- 包含点号(.)的查询
- 查询以($)开头的字段
- 子文档字段查询
$getField
聚合运算符返回文档中指定字段的值,字段名可以包含点.
或以$
开头。如果不指定,$getField
将$$CURRENT
作为字段返回。
语法
{$getField: {field: <String>,input: <Object>}
}
字段说明:
- field,字符串类型,指定数据对象中要返回的字段,可以是字符串表达式,如果字段以
$
符号开头,则需要把字段名放在$literal
表达式返回。 - input,对象类型,缺省值为
$$CURRENT
,可以是一个包含了指定字段值的表达式,input
必须要能解析为一个对象、缺失、空或未定义,如果省略,默认为当前管道中处理的文档$$CURRENT
。
从$$CURRENT
返回字段值的简单写法如下:
{$getField: <String>
}
使用
- 如果
field
不是字符串类型,$getField
返回错误 - 如果
input
对象中不存在field
或者未指定input
时$$CURRENT
不不存在field
字段,$getField
返回missing
- 如果
input
计算结果为missing
、undefined
或null
,$getField
返回null
- 如果
input
计算结果不是对象、missing
、undefined
或null
,则$getField
返回错误 $getField
不会隐式遍历对象或数组,例如:$getField
执行时会将field
为a.b.c
认为是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
运算符查找价格price
大于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
需要明确的field
和input
参数,因为$small
字段是子文档的一部分- 因为字段名前面有
$
符号,$getField
使用$literal
来计算"$small"
输出结果如下:
[{_id: 3,item: 'sun dress','price.usd': 199.99,quantity: { '$large': 45, '$medium': 40, '$small': 5 }}
]