Docs Menu
Docs Home
/
MongoDB マニュアル
/ / /

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

ドキュメントの 1 つにnameフィールドがないため、次の $replaceRoot 操作は失敗します。

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

エラーを回避するには、$mergeObjects を使用して name ドキュメントを次に挙げるような何らかのデフォルト ドキュメントにマージします。

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

あるいは、ドキュメントを$replaceRoot ステージに渡す前にドキュメント フィールドの存在を確認する $match ステージを含めることで、name フィールドがないドキュメントをスキップすることもできます。

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

次の操作では、90 以上の grade フィールドを持つ埋め込みドキュメントが最上位レベルに引き上げられます。

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_name フィールドと last_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

項目一覧