$replaceRoot (aggregation)
On this page本页内容
Definition定义
$replaceRoot
-
Replaces the input document with the specified document.用指定的文档替换输入文档。The operation replaces all existing fields in the input document, including the该操作将替换输入文档中的所有现有字段,包括_id
field._id
字段。You can promote an existing embedded document to the top level, or create a new document for promotion (see example).您可以将现有嵌入文档升级到顶级,也可以创建新文档进行升级(请参见示例)。NoteStarting in version 4.2, MongoDB adds a new aggregation pipeline stage,从4.2版本开始,MongoDB添加了一个新的聚合管道阶段$replaceWith
.$replaceWith
。The$replaceWith
stage peforms the same action as the$replaceRoot
stage, but the stages have different forms.$replaceWith
阶段执行与$replaceRoot
阶段相同的操作,但阶段具有不同的形式。The$replaceRoot
stage has the following form:$replaceRoot
阶段具有以下形式:{ $replaceRoot: { newRoot: <replacementDocument> } }
The replacement document can be any valid expression that resolves to a document.替换文档可以是解析为文档的任何有效表达式。The stage errors and fails if如果<replacementDocument>
is not a document.<replacementDocument>
不是文档,则阶段将出错并失败。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
Behavior行为
If the 如果<replacementDocument>
is not a document, $replaceRoot
errors and fails.<replacementDocument>
不是文档,$replaceRoot
将出错并失败。
If the 如果<replacementDocument>
resolves to a missing document (i.e. the document does not exist), $replaceRoot
errors and fails. For example, create a collection with the following documents:<replacementDocument>
解析为缺少文档(即文档不存在),$replaceRoot
将出错并失败。例如,使用以下文档创建集合:
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" },
])
Then the following 然后,由于其中一个文档没有$replaceRoot
operation fails because one of the documents does not have the name
field:name
字段,以下$replaceRoot
操作失败:
db.collection.aggregate([
{ $replaceRoot: { newRoot: "$name" } }
])
To avoid the error, you can use 为了避免错误,您可以使用$mergeObjects
to merge the name
document into some default document; $mergeObjects
将name
文档合并到某个默认文档中;for example:例如:
db.collection.aggregate([
{ $replaceRoot: { newRoot: { $mergeObjects: [ { _id: "$_id", first: "", last: "" }, "$name" ] } } }
])
Alternatively, you can skip the documents that are missing the 或者,您可以跳过缺少name
field by including a $match
stage to check for existence of the document field before passing documents to the $replaceRoot
stage:name
字段的文档,方法是在将文档传递到$replaceRoot
阶段之前,包括$match
阶段以检查文档字段的存在:
db.collection.aggregate([
{ $match: { name : { $exists: true, $not: { $type: "array" }, $type: "object" } } },
{ $replaceRoot: { newRoot: "$name" } }
])
Or, you can use 或者,您可以使用$ifNull
expression to specify some other document to be root; $ifNull
表达式指定其他文档为根文档;for example:例如:
db.collection.aggregate([
{ $replaceRoot: { newRoot: { $ifNull: [ "$name", { _id: "$_id", missingName: true} ] } } }
])
Examples实例
$replaceRoot
with an Embedded Document Field使用嵌入式文档字段$replaceRoot
$replaceRoot
with an Embedded Document FieldA collection named 名为people
contains the following documents:people
的集合包含以下文档:
{ "_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 }
The following operation uses the 以下操作使用$replaceRoot
stage to replace each input document with the result of a $mergeObjects
operation. $replaceRoot
阶段将每个输入文档替换为$mergeObjects
操作的结果。The $mergeObjects
expression merges the specified default document with the pets
document.$mergeObjects
表达式将指定的默认文档与pets
文档合并。
db.people.aggregate( [
{ $replaceRoot: { newRoot: { $mergeObjects: [ { dogs: 0, cats: 0, birds: 0, fish: 0 }, "$pets" ] }} }
] )
The operation returns the following results:该操作返回以下结果:
{ "dogs" : 2, "cats" : 1, "birds" : 0, "fish" : 0 }
{ "dogs" : 0, "cats" : 1, "birds" : 0, "fish" : 3 }
{ "dogs" : 0, "cats" : 0, "birds" : 0, "fish" : 0 }
$replaceRoot
with a Document Nested in an Array具有嵌套在数组中的文档
A collection named 一个名为students
contains the following documents: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 }
]
}
])
The following operation promotes the embedded document(s) with the 以下操作将grade
field greater than or equal to 90
to the top level:grade
字段大于或等于90
的嵌入文档提升到最高级别:
db.students.aggregate( [
{ $unwind: "$grades" },
{ $match: { "grades.grade" : { $gte: 90 } } },
{ $replaceRoot: { newRoot: "$grades" } }
] )
The operation returns the following results:该操作返回以下结果:
{ "test" : 3, "grade" : 95, "mean" : 85, "std" : 6 }
{ "test" : 1, "grade" : 90, "mean" : 75, "std" : 6 }
{ "test" : 3, "grade" : 91, "mean" : 85, "std" : 4 }
$replaceRoot
with a newly created document使用新创建的文档$replaceRoot
$replaceRoot
with a newly created documentYou can also create new documents as part of the 您还可以创建新文档作为$replaceRoot
stage and use them to replace all the other fields.$replaceRoot
阶段的一部分,并使用它们来替换所有其他字段。
A collection named 名为contacts
contains the following documents:contacts
的集合包含以下文档:
{ "_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" }
The following operation creates a new document out of the 以下操作将从first_name
and last_name
fields.first_name
和last_name
字段中创建一个新文档。
db.contacts.aggregate( [
{
$replaceRoot: {
newRoot: {
full_name: {
$concat : [ "$first_name", " ", "$last_name" ]
}
}
}
}
] )
The operation returns the following results:该操作返回以下结果:
{ "full_name" : "Gary Sheffield" }
{ "full_name" : "Nancy Walker" }
{ "full_name" : "Peter Sumner" }
$replaceRoot
with a New Document Created from $$ROOT
and a Default Document带有从$$ROOT
创建的新文档和默认文档的$replaceRoot
$replaceRoot
with a New Document Created from $$ROOT
and a Default DocumentCreate a collection named 使用以下文档创建名为contacts
with the following documents: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", home: "987-654-3210", email: "beo@example.net" }
] )
The following operation uses 以下操作使用$replaceRoot
with $mergeObjects
to output current documents with default values for missing fields:$replaceRoot
和$mergeObjects
来输出缺少字段的具有默认值的当前文档:
db.contacts.aggregate( [
{ $replaceRoot:
{ newRoot:
{ $mergeObjects:
[
{ _id: "", name: "", email: "", cell: "", home: "" },
"$$ROOT"
]
}
}
}
] )
The aggregation returns the following documents:聚合返回以下文档:
{
_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'
}