MongoDB聚合运算符:$setField
文章目录
$setField聚合运算符用于添加、更新、删除文档中的指定字段,可以用于字段名称包含
.或以
$开头的字段。从5.0版本开始支持。
语法
{
$setField: {
field: <String>,
input: <Object>,
value: <Expression>
}
}
字段说明
| 字段 | 类型 | 说明 |
|---|---|---|
field |
字符串 | 要添加、更新或删除的输入对象中的字段,字段可以是字符串表达式 |
input |
对象 | 指定要更新的文档,input必须是可以解析为文档的对象、缺失、空或未定义 |
value |
表达式 | 要分配给field的值,value可以是任何有效的表达式,如果设置为$$REMOVE,则会从输入文档中删除字段 |
使用
-
如果
input的计算结果为缺失、undefined或null,则$setField返回null并且不更新input。 -
如果
input的计算结果为除对象、缺失、undefined或null之外的任何内容,则$setField将返回错误。 -
如果
field解析为字符串常量以外的任何内容,则$setField返回错误。 -
如果
input中不存在field,$setField将添加一个field。 -
$setField不会隐式遍历对象或数组,例如$setField将字段值"a.b.c"计算为顶级字段"a.b.c",而不是嵌套字段{ "a": { "b": { "c": } } }。 -
$unsetField是输入值为$$REMOVE的$setField的别名。以下表达式是等效的:{ $setField: { field: <field name>, input: “$$ROOT”, value: "$$REMOVE" } } { $unsetField: { field: <field name>, input: “$$ROOT” } }
举例
添加包含点号 (.) 的字段
使用下面的脚本创建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 }
] )
下面的聚合操作使用 $replaceWith 管道阶段和 $setField 操作符为每个文档添加一个新字段 "price.usd"。"price.usd" 的值将等于每个文档 "price"的值。最后,使用 $unset 管道阶段删除 "price"字段:
db.inventory.aggregate( [
{
$replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: "$price"
} } },
{
$unset: "price" }
] )
操作返回下面的结果:
[
{
_id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
{
_id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{
_id: 3, item:
MongoDB$setField操作详解:动态字段管理,

最低0.47元/天 解锁文章
852

被折叠的 条评论
为什么被折叠?



