在上一篇?mongodb Aggregation聚合操作之$sortByCount?中詳細(xì)介紹了mongodb聚合操作中的$sortByCount使用以及參數(shù)細(xì)節(jié)砸泛。本篇將開始介紹Aggregation聚合操作中的$replaceRoot操作。
說明:
用指定的文檔替換輸入文檔裤翩。該操作將替換輸入文檔中的所有現(xiàn)有字段嚷往,包括_id字段。您可以將現(xiàn)有的嵌入文檔提升到頂層吧彪,或者創(chuàng)建一個用于提升的新文檔
語法:
{ $replaceRoot: { newRoot: <replacementDocument> } }
1.?示例
1.1.?簡單示例
初始化數(shù)據(jù):
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將名稱文檔合并到某個默認(rèn)文檔中解決這個錯誤
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表達(dá)式指定其他一些文檔為根文檔;例如:
db.replaceRootExample.aggregate([
???{ $replaceRoot: { newRoot: { $ifNull: [ "$name", { _id: "$_id", missingName: true} ] } } }
])
1.2.?嵌入文檔字段示例
初始化數(shù)據(jù):
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" ] }} }
] )
結(jié)果:
{ "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.?數(shù)組中嵌套文檔示例
初始化數(shù)據(jù):
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" } }
] )
結(jié)果:
{ "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.?連接原有文檔中多個作為一個字段
初始化數(shù)據(jù):
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和默認(rèn)文檔創(chuàng)建的新文檔
初始化字段:
db.contacts.insert([
???{ "_id" : 1, name: "Fred", email: "fred@example.net" },
???{ "_id" : 2, name: "Frank N. Stine", cell: "012-345-9999" },
???{ "_id" : 3, name: "Gren Dell", home: "987-654-3210", email: "beo@example.net" }
]);
示例:
db.contacts.aggregate([
???{ $replaceRoot: { newRoot: { $mergeObjects: [ { _id: "", name: "", email: "", cell: "", home: "" }, "$$ROOT" ] } } }
])
結(jié)果:
{ "_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" }