Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

$replaceRoot(聚合)

在此页面上

  • 定义
  • 行为
  • 举例
$replaceRoot

将输入文档替换为指定的文档。该操作会替换输入文档中的所有现有字段,包括 _id 字段。您可以将现有的嵌入式文档提升到顶层,或创建新文档进行提升(请参阅示例)。

注意

您也可以使用 $replaceWith阶段。 $replaceWith阶段执行与$replaceRoot阶段相同的动作,但阶段具有不同的形式。

$replaceRoot 阶段具有以下形式:

{ $replaceRoot: { newRoot: <replacementDocument> } }

替换文档可以是任何解析为文档的有效表达式。如果 <replacementDocument> 不是文档,则该阶段会出错并失败。有关表达式的更多信息,请参阅表达式运算符。

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

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

然后,以下 $replaceRoot 操作会失败,因为其中一个文档没有 name 字段:

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

为了避免该错误,您可以使用$mergeObjectsname文档合并到某个默认文档中;例如:

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

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

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

也可以使用 $ifNull 表达式指定其他文档作为根文档;例如:

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

一个名为 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 }

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

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 }

一个名为 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 } } },
{ $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 }

您还可以在 $replaceRoot 阶段创建新文档,并用它们替换所有其他字段。

一个名为 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" }

以下操作根据 first_namelast_name 字段创建新文档。

db.contacts.aggregate( [
{
$replaceRoot: {
newRoot: {
full_name: {
$concat : [ "$first_name", " ", "$last_name" ]
}
}
}
}
] )

操作返回以下结果:

{ "full_name" : "Gary Sheffield" }
{ "full_name" : "Nancy Walker" }
{ "full_name" : "Peter Sumner" }

使用以下文档创建名为 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" }
] )

以下操作使用 $replaceRoot$mergeObjects 输出当前文档,其中包含缺失字段的默认值:

db.contacts.aggregate( [
{ $replaceRoot:
{ newRoot:
{ $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'
}

后退

$redact

来年

$replaceWith

在此页面上