$replaceRoot stage do in a MongoDB aggregation pipeline?$replaceRoot replaces the current document with a specified embedded document. It helps restructure documents by promoting a nested document to the top level.
$replaceRoot?You use the newRoot field inside $replaceRoot to specify the embedded document that will become the new root.
{ _id: 1, info: { name: 'Alice', age: 30 } }, what will { $replaceRoot: { newRoot: '$info' } } produce?The output document will be { name: 'Alice', age: 30 }. The info subdocument becomes the whole document.
$replaceRoot be used to flatten nested documents?Yes, by replacing the root with a nested document, you effectively flatten the structure by removing the outer layers.
newRoot field in $replaceRoot does not exist in the document?The aggregation will return null for that document, effectively removing it from the output if followed by a $match or similar stage.
$replaceRoot in MongoDB?$replaceRoot changes the root document to a specified embedded document, restructuring the output.
$replaceRoot to specify the new root document?The newRoot field defines the embedded document to become the new root.
{ _id: 2, details: { city: 'NY', zip: 10001 } }, what does { $replaceRoot: { newRoot: '$details' } } output?The details subdocument becomes the whole document after $replaceRoot.
newRoot path does not exist in a document, what is the result?The document becomes null because the path does not exist.
$replaceRoot be used to promote a nested document to the top level?$replaceRoot is designed to promote nested documents to the root level.
$replaceRoot works in MongoDB aggregation and give a simple example.newRoot does not exist in a document during $replaceRoot?