AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

$replaceRoot (집계 단계)

$replaceRoot

Replaces the input document with the specified document. The operation replaces all existing fields in the input document, including the _id field. You can promote an existing embedded document to the top level, or create a new document for promotion (see example).

참고

You can also use the $replaceWith stage. The $replaceWith stage peforms the same action as the $replaceRoot stage, but the stages have different forms.

The $replaceRoot stage has the following form:

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

대체 문서는 문서로 해석되는 모든 유효한 표현식 일 수 있습니다. <replacementDocument> 이(가) 문서가 아닌 경우 스테이지 오류가 발생하고 실패합니다. 표현식에 대한 자세한 내용은 표현식을 참조하세요 .

If the <replacementDocument> is not a document, $replaceRoot errors and fails.

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:

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:

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: '' }
]

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:

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

The following operation uses the $replaceRoot stage to replace each input document with the result of a $mergeObjects operation. The $mergeObjects expression merges the specified default document with the pets document.

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라는 이름의 컬렉션에 다음 문서가 포함되어 있습니다.

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

The following operation uses $replaceRoot with $mergeObjects to output current documents with default values for missing fields:

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

To learn more about related pipeline stages, see the $replaceRoot guide.

이 페이지 평가하기