“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

$replaceWith(聚合)

在此页面上

  • 定义
  • 行为
  • 举例
$replaceWith

将输入文档替换为指定文档。该操作会替换输入文档中的所有现有字段,包括_id字段。使用 $replaceWith ,您可以将嵌入式文档提升到顶层。您还可以指定一个新文档作为替换文档。

$replaceWith阶段执行与 $replaceRoot阶段相同的动作,但阶段具有不同的形式。

$replaceWith 阶段具有以下形式:

{ $replaceWith: <replacementDocument> }

替换文档可以是任何解析为文档的有效表达式。有关表达式的更多信息,请参阅表达式运算符。

如果 <replacementDocument> 不是文档,$replaceWith 会出错并失败。

如果 <replacementDocument> 解析为缺失的文档(即该文档不存在),则 $replaceWith 会出错并失败。例如,使用以下文档创建集合:

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

或者,您可以跳过缺少 name 字段的文档,方法是:纳入 $match 阶段,以检查是否存在该文档字段,然后将该文档传递至 $replaceWith 阶段:

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

以下操作使用 $replaceWith 阶段将每个输入文档替换为 $mergeObjects 操作的结果。$mergeObjects 表达式将指定的默认文档与 pets 文档合并。

db.people.aggregate( [
{ $replaceWith: { $mergeObjects: [ { dogs: 0, cats: 0, birds: 0, fish: 0 }, "$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 }

一个名为 students 的集合包含以下文档:

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 }

使用以下文档填充的示例集合 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 阶段创建新文档。$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") }

示例集合 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" } }
] )
第一个阶段:

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

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

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

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

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

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

$replaceWith 采用指定的键 k 和值 v 对,通过 $arrayToObjectitems2 转换成文档,并将该文档输出到下一阶段。例如:

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

该聚合返回以下文档:

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

使用以下文档创建名为 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'
}
← $replaceRoot(聚合)

在此页面上