MongoDB聚合运算符:$setField

embedded/2024/10/18 6:12:51/

MongoDB聚合运算符:$setField

文章目录

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

http://www.ppmy.cn/embedded/10122.html

相关文章

自然语言处理(NLP)技术

自然语言处理&#xff08;NLP&#xff09;技术是一种处理和分析人类语言的技术&#xff0c;它可以帮助计算机理解、解释和生成自然语言。 以下是一些常见的使用NLP技术的例子&#xff1a; 1. 机器翻译&#xff1a;NLP技术可以将一种语言翻译成另一种语言&#xff0c;比如将英…

Jenkins CI/CD 持续集成专题三 Cocoapods /Cocoapods Packager 问题汇总

执行pod package xxx.podspec --force --no-mangle --exclude-deps --verbose 报错 warning: Building targets in manual order is deprecated - check "Parallelize build for command-line builds" in the project editor, or set DISABLE_MANUAL_TARGET_ORDER_BU…

第十章 Linux系统安全及应用

目录 一、账号安全控制 1、账号安全基本措施 &#xff08;1&#xff09;系统账号清理 &#xff08;2&#xff09;密码安全控住 &#xff08;3&#xff09;命令历史限制 &#xff08;4&#xff09;终端自动注销 2、使用su命令切换用户 &#xff08;1&#xff09;限制使用…

【存储】cosbench对象存储测试工具

目录 简略说明 原理 用法 详细说明 简介 用法 一 安装 二 简单验证 三 编写配置文件 四 提交配置文件下IO 五 测试结果查看 结果概览 查看详情 每秒钟的io情况查看 工作负载配置 参数配置&#xff08;controller和driver&#xff09; 查看错误的方法和错误记录 查看错误的方法 …

Acer宏碁掠夺者战斧300笔记本电脑PH315-52工厂模式原装Win10系统安装包 恢复出厂开箱状态 带恢复重置

宏碁掠夺者PH315-52原厂Windows10工厂包镜像下载&#xff0c;预装oem系统 链接&#xff1a;https://pan.baidu.com/s/1grmJzz6nW1GOaImY_ymXGw?pwdi286 提取码&#xff1a;i286 原厂W10系统自带所有驱动、PredatorSense风扇键盘控制中心、Office办公软件、出厂主题壁纸、系统…

STM32F1串口

文章目录 1 数据通信的基础概念1.11.21.31.41.5 2 串口(RS-232&#xff09;2.12.22.32.42.5 3 STM32的USART3.13.23.33.53.9 USART寄存器介绍 4 HAL库外设初始化MSP回调机制5 HAL库中断回调机制6 USART/UART异步通信配置步骤 &#xff08;包括HAL库相关函数&#xff09;6.16.26…

RestFul 风格(SpringMVC学习笔记三)

1、什么是Restful风格&#xff1a; Restful就是一个资源定位及资源操作的风格。不是标准也不是协议&#xff0c;只是一种风格。基于这个风格设计的软件可以更简洁&#xff0c;更有层次&#xff0c;更易于实现缓存等机制。 2、使用Restful风格 接上一个笔记的测试类 package…

【Web】HNCTF 2022 题解(全)

目录 Week1 Interesting_include 2048 easy_html What is Web Interesting_http easy_upload Week2 ez_SSTI easy_include ez_ssrf Canyource easy_unser easy_sql ohmywordpress Week3 ssssti Fun_php ez_phar QAQ_1inclu4e logjjjjlogjjjj …