MongoDB聚合运算符:$setField

本文详细介绍了MongoDB的$setField聚合运算符,包括其语法、字段类型、使用示例,展示了如何在文档中添加、更新和删除包含点号和以$开头的字段,以及其与$unsetField的异同。
摘要由CSDN通过智能技术生成

MongoDB聚合运算符:$setField


$setField聚合运算符用于添加、更新、删除文档中的指定字段,可以用于字段名称包含 .或以 $开头的字段。从5.0版本开始支持。

语法

{
  $setField: {
    field: <String>,
    input: <Object>,
    value: <Expression>
  }
}

字段说明

字段类型说明
field字符串要添加、更新或删除的输入对象中的字段,字段可以是字符串表达式
input对象指定要更新的文档,input必须是可以解析为文档的对象、缺失、空或未定义
value表达式要分配给field的值,value可以是任何有效的表达式,如果设置为$$REMOVE,则会从输入文档中删除字段

使用

  • 如果input的计算结果为缺失、undefinednull,则 $setField 返回 null 并且不更新input

  • 如果input的计算结果为除对象、缺失、undefinednull 之外的任何内容,则 $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: 'sun dress', qty: 250, 'price.usd': 199.99 },
  { _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
  { _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.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 }
] )

以下聚合操作使用 $replaceWith 管道阶段以及 $setField$literal 运算符向每个文档添加新字段"$price""$price"的值等于每个文档中"price"的值,最后使用 $unset 管道阶段删除"price"字段:

db.inventory.aggregate( [
   { $replaceWith: {
        $setField: {
           field: { $literal: "$price" },
           input: "$$ROOT",
           value: "$price"
   } } },
   { $unset: "price" }
] )

操作返回下面的结果:

[
  { _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
  { _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
  { _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
  { _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
  { _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
]

更新包含(.)的字段

使用下面的脚本创建inventory集合:

db.inventory.insertMany( [
   { _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
   { _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
   { _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
   { _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
   { _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
] )

下面的聚合操作使用 $match 管道阶段查找制定的文档,并使用 $replaceWith 管道阶段和 $setField 运算符更新匹配文档中的"price.usd"字段:

db.inventory.aggregate( [
   { $match: { _id: 1 } },
   { $replaceWith: {
        $setField: {
           field: "price.usd",
           input: "$$ROOT",
           value: 49.99
    } } }
] )

操作返回下面的结果:

[
  { _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 49.99 }
]

更新以($)开头的字段

使用下面的脚本创建inventory集合:

db.inventory.insertMany([
   { _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
   { _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
   { _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
   { _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
   { _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
] )

以下操作使用 $match 管道阶段来查找文档,并使用 $replaceWith 管道阶段以及 $setField$literal 运算符来更新匹配文档中的"$price"字段:

db.inventory.aggregate( [
   { $match: { _id: 1 } },
   { $replaceWith: {
        $setField: {
           field: { $literal: "$price" },
           input: "$$ROOT",
           value: 49.99
   } } }
] )

操作返回下面的结果:

[
  { _id: 1, item: 'sweatshirt', qty: 300, '$price': 49.99 }
]

删除包含(.)的字段

使用下面的脚本创建inventory集合:

db.inventory.insertMany([
   { _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
   { _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
   { _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
   { _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
   { _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
] )

以下操作使用 $replaceWith 管道阶段、$setField 操作符和 $$REMOVE 从文档中删除 "price.usd"字段::

db.inventory.aggregate( [
   { $replaceWith:  {
        $setField: {
           field: "price.usd",
           input: "$$ROOT",
           value: "$$REMOVE"
   } } }
] )

操作返回下面的结果:

[
  { _id: 1, item: 'sweatshirt', qty: 300 },
  { _id: 2, item: 'winter coat', qty: 200 },
  { _id: 3, item: 'sun dress', qty: 250 },
  { _id: 4, item: 'leather boots', qty: 300 },
  { _id: 5, item: 'bow tie', qty: 180 }
]

使用 $unsetField 也可以完成查询返回相同的结果:

db.inventory.aggregate( [
   { $replaceWith:  {
        $unsetField: {
           field: "price.usd",
           input: "$$ROOT"
   } } }
] )

删除以($)开头的字段

使用下面的脚本创建inventory集合:

db.inventory.insertMany( [
   { _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
   { _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
   { _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
   { _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
   { _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
} )

以下操作使用 $replaceWith 管道阶段、$setField$literal 运算符以及 $$REMOVE 删除文档中的"$price"字段:

db.inventory.aggregate( [
   { $replaceWith: {
        $setField: {
           field: { $literal: "$price" },
           input: "$$ROOT",
           value: "$$REMOVE"
   } } }
] )

操作返回下面的结果:

[
  { _id: 1, item: 'sweatshirt', qty: 300 },
  { _id: 2, item: 'winter coat', qty: 200 },
  { _id: 3, item: 'sun dress', qty: 250 },
  { _id: 4, item: 'leather boots', qty: 300 },
  { _id: 5, item: 'bow tie', qty: 180 }
]

使用 $unsetField 可以实现相同的效果:

db.inventory.aggregate( [
  { $replaceWith: {
       $unsetField: {
          field: { $literal: "$price" },
          input: "$$ROOT"
  } } }
] )
  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原子星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值