在上一篇 mongodb Aggregation聚合操作之$sortByCount 中详细介绍了mongodb聚合操作中的$sortByCount使用以及参数细节。本篇将开始介绍Aggregation聚合操作中的$replaceRoot操作。
说明:
用指定的文档替换输入文档。该操作将替换输入文档中的所有现有字段,包括_id字段。您可以将现有的嵌入文档提升到顶层,或者创建一个用于提升的新文档
语法:
{ $replaceRoot: { newRoot:
1. 示例
1.1. 简单示例
初始化数据:
db.replaceRootExample.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" },
])
注意:【如果文档记录中有replacementDocument的字段不存在会报错,例如下面这个示例,在第四条记录中name字段不存在】
示例:
db.replaceRootExample.aggregate([
{ $replaceRoot: { newRoot: "$name" } }
])
会报:MongoError: 'newRoot' expression must evaluate to an object, but resulting value was: MISSING. Type of resulting value: 'missing'. Input document: {}
可以使用$mergeObjects将名称文档合并到某个默认文档中解决这个错误
db.replaceRootExample.aggregate([
{ $replaceRoot: { newRoot: { $mergeObjects: [ { _id: "$_id", first: "", last: "" }, "$name" ] } } }
])
或者,您可以跳过缺少name字段的文档,通过包含$match阶段来检查文档字段是否存在,然后再将文档传递到$replaceRoot阶段:
db.replaceRootExample.aggregate([
{ $match: { name : { $exists: true, $not: { $type: "array" }, $type: "object" } } },
{ $replaceRoot: { newRoot: "$name" } }
])
或者,您可以使用$ifNull表达式指定其他一些文档为根文档;例如:
db.replaceRootExample.aggregate([
{ $replaceRoot: { newRoot: { $ifNull: [ "$name", { _id: "$_id", missingName: true} ] } } }
])
1.2. 嵌入文档字段示例
初始化数据:
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 }])
示例:
db.people.aggregate( [
{ $replaceRoot: { newRoot: { $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 }
1.3. 数组中嵌套文档示例
初始化数据:
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 }
]
}
])
示例:
db.students.aggregate( [
{ $unwind: "$grades" },
{ $match: { "grades.grade" : { $gte: 90 } } },
{ $replaceRoot: { newRoot: "$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.4. 连接原有文档中多个作为一个字段
初始化数据:
db.contacts.insertMany([{ "_id" : 1, "first_name" : "Gary", "last_name" : "Sheffield", "city" : "New York" },
{ "_id" : 2, "first_name" : "Nancy", "last_name" : "Walker", "city" : "Anaheim" },
{ "_id" : 3, "first_name" : "Peter", "last_name" : "Sumner", "city" : "Toledo" }])
示例:
db.contacts.aggregate( [
{
$replaceRoot: {
newRoot: {
full_name: {
$concat : [ "$first_name", " ", "$last_name" ]
}
}
}
}
] )
{ "full_name" : "Gary Sheffield" }
{ "full_name" : "Nancy Walker" }
{ "full_name" : "Peter Sumner" }
1.5. 使用从$$ROOT和默认文档创建的新文档
初始化字段:
db.contacts.insert([
{ "_id" : 1, name: "Fred", email: "[email protected]" },
{ "_id" : 2, name: "Frank N. Stine", cell: "012-345-9999" },
{ "_id" : 3, name: "Gren Dell", home: "987-654-3210", email: "[email protected]" }
]);
示例:
db.contacts.aggregate([
{ $replaceRoot: { newRoot: { $mergeObjects: [ { _id: "", name: "", email: "", cell: "", home: "" }, "$$ROOT" ] } } }
])
结果:
{ "_id" : 1, "name" : "Fred", "email" : "[email protected]", "cell" : "", "home" : "" }
{ "_id" : 2, "name" : "Frank N. Stine", "email" : "", "cell" : "012-345-9999", "home" : "" }
{ "_id" : 3, "name" : "Gren Dell", "email" : "[email protected]", "cell" : "", "home" : "987-654-3210" }