AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

$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" ] } } }])
[ { _id: 1, first: 'John', last: 'Backus' }, { _id: 2, first: 'John', last: 'McCarthy' }, { _id: 3, first: 'Grace', last: 'Hopper' }, { _id: 4, first: '', last: '' }]

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

db.collection.aggregate([ { $match: { name : { $exists: true, $not: { $type: "array" }, $type: "object" } } }, { $replaceRoot: { newRoot: "$name" } }])
[ { first: 'John', last: 'Backus' }, { first: 'John', last: 'McCarthy' }, { first: 'Grace', last: 'Hopper' }]

または、$ifNull 式を使用して、次に挙げるような他のドキュメントをルートに指定しても構いません。

db.collection.aggregate([ { $replaceRoot: { newRoot: { $ifNull: [ "$name", { _id: "$_id", missingName: true} ] } } }])
[ { first: 'John', last: 'Backus' }, { first: 'John', last: 'McCarthy' }, { first: 'Grace', last: 'Hopper' }, { _id: 4, missingName: true }]

people という名前のコレクションには次のドキュメントが含まれています。

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

次の操作では、$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 という名前のコレクションには次のドキュメントが含まれています。

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

次の操作では、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' }]

関連するパイプラインステージの詳細については、「$replaceRoot」ガイドを参照してください。

このページを評価