当前位置: 首页 > news >正文

MongoDB聚合:$replaceWith

r e p l a c e W i t h ‘ 可以将输入文档替换为指定的文档。该操作可以替换输入文档的所有字段,包括 ‘ i d ‘ 字段。使用 ‘ replaceWith`可以将输入文档替换为指定的文档。该操作可以替换输入文档的所有字段,包括`_id`字段。使用` replaceWith可以将输入文档替换为指定的文档。该操作可以替换输入文档的所有字段,包括id字段。使用replaceWith还可以将内嵌文档提升到最顶级,也可以把它替换掉。替换文档可以是任何能够解析为文档的有效表达式。 r e p l a c e W i t h ‘ 与 ‘ replaceWith`与` replaceWithreplaceRoot`有很多相似点,但也有一些不同点。

语法

{ $replaceWith: <replacementDocument> }

使用

如果<replacementDocument>不是一个文档或者被解析为一个错误的文档(如:文档不存在),都会失败,例如:使用下面的文档创建一个集合:

db.collection.insertMany([{ "_id": 1, "name" : { "first" : "John", "last" : "Backus" } },{ "_id": 2, "name" : { "first" : "John", "last" : "McCarthy" } },{ "_id": 3, "name": { "first" : "Grace", "last" : "Hopper" } },{ "_id": 4, "firstname": "Ole-Johan", "lastname" : "Dahl" },
])

下面的$replaceWith会操作失败,因为最后一个文档缺少name字段:

db.collection.aggregate([{ $replaceWith: "$name" }
])

要避免上面的错误,可以使用$mergeObjectsname文档与某个缺省文档合并,如:

db.collection.aggregate([{ $replaceWith: { $mergeObjects: [ { _id: "$_id", first: "", last: "" }, "$name" ] } }
])

也可以使用$match阶段,在$replaceWith之前筛选掉name字段异常的数据:

db.collection.aggregate([{ $match: { name : { $exists: true, $not: { $type: "array" }, $type: "object" } } },{ $replaceWith: "$name" }
])

亦或者使用$ifNull表达式来指定其它的文档,如:

db.collection.aggregate([{ $replaceWith: { $ifNull: [ "$name", { _id: "$_id", missingName: true} ] } }
])

举例:

替换内嵌文档字段

使用下面的语句创建一个people集合:

db.people.insertMany([{ "_id" : 1, "name" : "Arlene", "age" : 34, "pets" : { "dogs" : 2, "cats" : 1 } },{ "_id" : 2, "name" : "Sam", "age" : 41, "pets" : { "cats" : 1, "fish" : 3 } },{ "_id" : 3, "name" : "Maria", "age" : 25 }
])

下面的操作对people集合中的内嵌字段pets合并后进行替换,结果为:

{ "dogs" : 2, "cats" : 1, "birds" : 0, "fish" : 0 }
{ "dogs" : 0, "cats" : 1, "birds" : 0, "fish" : 3 }
{ "dogs" : 0, "cats" : 0, "birds" : 0, "fish" : 0 }

使用嵌套的数组中文档替换

student集合有下面内容:

db.students.insertMany([{"_id" : 1,"grades" : [{ "test": 1, "grade" : 80, "mean" : 75, "std" : 6 },{ "test": 2, "grade" : 85, "mean" : 90, "std" : 4 },{ "test": 3, "grade" : 95, "mean" : 85, "std" : 6 }]},{"_id" : 2,"grades" : [{ "test": 1, "grade" : 90, "mean" : 75, "std" : 6 },{ "test": 2, "grade" : 87, "mean" : 90, "std" : 3 },{ "test": 3, "grade" : 91, "mean" : 85, "std" : 4 }]}
])

下面的操作会将grade字段大于或等于90的内嵌文档提升到顶层:

db.students.aggregate( [{ $unwind: "$grades" },{ $match: { "grades.grade" : { $gte: 90 } } },{ $replaceWith: "$grades" }
] )

结果:

{ "test" : 3, "grade" : 95, "mean" : 85, "std" : 6 }
{ "test" : 1, "grade" : 90, "mean" : 75, "std" : 6 }
{ "test" : 3, "grade" : 91, "mean" : 85, "std" : 4 }

使用新创建的文档替换

例1

集合sales包含以下文档:

db.sales.insertMany([{ "_id" : 1, "item" : "butter", "price" : 10, "quantity": 2, date: ISODate("2019-03-01T08:00:00Z"), status: "C" },{ "_id" : 2, "item" : "cream", "price" : 20, "quantity": 1, date: ISODate("2019-03-01T09:00:00Z"), status: "A" },{ "_id" : 3, "item" : "jam", "price" : 5, "quantity": 10, date: ISODate("2019-03-15T09:00:00Z"), status: "C" },{ "_id" : 4, "item" : "muffins", "price" : 5, "quantity": 10, date: ISODate("2019-03-15T09:00:00Z"), status: "C" }
])

假设为了要计算已完成销售的总金额,即查找所有状态为C的销售记录,使用$replaceWith阶段创建新文档计算总金额,并使用变量NOW获取当前时间:

db.sales.aggregate([{ $match: { status: "C" } },{ $replaceWith: { _id: "$_id", item: "$item", amount: { $multiply: [ "$price", "$quantity"]}, status: "Complete", asofDate: "$$NOW" } }
])

操作返回以下结果:

{ "_id" : 1, "item" : "butter", "amount" : 20, "status" : "Complete", "asofDate" : ISODate("2019-06-03T22:47:54.812Z") }
{ "_id" : 3, "item" : "jam", "amount" : 50, "status" : "Complete", "asofDate" : ISODate("2019-06-03T22:47:54.812Z") }
{ "_id" : 4, "item" : "muffins", "amount" : 50, "status" : "Complete", "asofDate" : ISODate("2019-06-03T22:47:54.812Z") }
例2

reportedsales集合按季度和地区填入报销售额信息,如下:

db.reportedsales.insertMany( [{ _id: 1, quarter: "2019Q1", region: "A", qty: 400 },{ _id: 2, quarter: "2019Q1", region: "B", qty: 550 },{ _id: 3, quarter: "2019Q1", region: "C", qty: 1000 },{ _id: 4, quarter: "2019Q2", region: "A", qty: 660 },{ _id: 5, quarter: "2019Q2", region: "B", qty: 500 },{ _id: 6, quarter: "2019Q2", region: "C", qty: 1200 }
] )

假设出于报告目的,想查看按季度报告的销售数据,例如

{ "_id" : "2019Q1", "A" : 400, "B" : 550, "C" : 1000 }

可以使用下面的聚合管道:

db.reportedsales.aggregate( [{ $addFields: { obj:  { k: "$region", v: "$qty" } } },{ $group: { _id: "$quarter", items: { $push: "$obj" } } },{ $project: { items2: { $concatArrays: [ [ { "k": "_id", "v": "$_id" } ], "$items" ] } } },{ $replaceWith: { $arrayToObject: "$items2" } }
] )
阶段1

$addFields阶段添加了一个新的obj文档字段,该字段将关键字k定义为区域值,将值v定义为该区域的数量。例如

{ "_id" : 1, "quarter" : "2019Q1", "region" : "A", "qty" : 400, "obj" : { "k" : "A", "v" : 400 } }
阶段2

$group阶段按季度分组,并使用$pushobj字段累加到一个新的items数组字段中。例如:

{ "_id" : "2019Q1", "items" : [ { "k" : "A", "v" : 400 }, { "k" : "B", "v" : 550 }, { "k" : "C", "v" : 1000 } ] }
阶段3

$project阶段使用$concatArrays创建一个新数组items2,其中包括_id信息和items数组中的元素:

{ "_id" : "2019Q1", "items2" : [ { "k" : "_id", "v" : "2019Q1" }, { "k" : "A", "v" : 400 }, { "k" : "B", "v" : 550 }, { "k" : "C", "v" : 1000 } ] }
阶段4

$replaceWith使用$arrayToObjectitems2转换成文档,使用kv键值对,并将文档输出到下一阶段。例如:

{ "_id" : "2019Q1", "A" : 400, "B" : 550, "C" : 1000 }

最终的执行结果:

{ "_id" : "2019Q1", "A" : 400, "B" : 550, "C" : 1000 }
{ "_id" : "2019Q2", "A" : 660, "B" : 500, "C" : 1200 }

使用$$ROOT创建的新文档和默认文档替换

创建contacts集合:

db.contacts.insertMany( [{ "_id" : 1, name: "Fred", email: "fred@example.net" },{ "_id" : 2, name: "Frank N. Stine", cell: "012-345-9999" },{ "_id" : 3, name: "Gren Dell", cell: "987-654-3210", email: "beo@example.net" }
] )

下面使用$replaceWith$mergeObjects输出当前文档中缺失字段的默认值:

db.contacts.aggregate( [{ $replaceWith:{ $mergeObjects:[{ _id: "", name: "", email: "", cell: "", home: "" },"$$ROOT"]}}
] )

返回下面的文档:

{_id: 1,name: 'Fred',email: 'fred@example.net',cell: '',home: ''
},
{_id: 2,name: 'Frank N. Stine',email: '',cell: '012-345-9999',home: ''
},
{_id: 3,name: 'Gren Dell',email: 'beo@example.net',cell: '',home: '987-654-3210'
}
http://www.lryc.cn/news/297848.html

相关文章:

  • Vue 进阶系列丨实现简易VueRouter
  • unity editor 编辑器 GUID localID LocalFileId 查找问题
  • 【Mybatis】从0学习Mybatis(2)
  • ChatGPT高效提问—prompt常见用法(续篇九)
  • echarts的title标题属性
  • 【HTML+CSS】使用CSS中的Position与z-index轻松实现一个简单的自定义标题栏效果
  • 从零开始:用 Rust 编写你的第一个 Web 服务
  • 机器学习复习(8)——逻辑回归
  • 深入解析MySQL 8:事务数据字典的变革
  • jquery写表格,通过后端传值,并合并单元格
  • 百家cms代审
  • 算法学习——LeetCode力扣二叉树篇3
  • 强制卸载挂载目录
  • HiveSQL——sum(if()) 条件累加
  • Linux命令行工具使用HTTP代理的方法详解
  • idea mavn 中途新建gitignore文件如何生效
  • Hadoop:认识MapReduce
  • 9.4 OpenGL帧缓冲:纹理和帧缓冲之间的反馈循环
  • 相机图像质量研究(6)常见问题总结:光学结构对成像的影响--对焦距离
  • fast.ai 机器学习笔记(二)
  • vue3 elementplus DateTimePicker 日期时间设置默认时间为当天
  • 2024年笔记--centos docker离线安装启动失败
  • 2024.2.10 DMS(数据库管理系统)初体验
  • zk集群--集群同步
  • 复习面经哦
  • c++ STL系列——(二)vector
  • STM32能够做到数据采集和发送同时进行吗?
  • 5.Swift常量
  • Linux运行级别 | 管理Linux服务
  • Nginx 配置 SSL证书