$replaceRoot (aggregation)

Definition

  • $replaceRoot

New in version 3.4.

Replaces the input document with the specified document. Theoperation replaces all existing fields in the input document,including the _id field. You can promote an existingembedded document to the top level, or create a new documentfor promotion (seeexample).

Note

Starting in version 4.2, MongoDB adds a new aggregation pipelinestage $replaceWith that is an alias for$replaceRoot.

The $replaceRoot stage has the following form:

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

The replacement document can be any valid expression that resolves to a document. The stageerrors and fails if <replacementDocument> is not a document. Formore information on expressions, see Expressions.

Behavior

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 andfails. For example, create a collection with the followingdocuments:

  1. db.collection.insertMany([
  2. { "_id": 1, "name" : { "first" : "John", "last" : "Backus" } },
  3. { "_id": 2, "name" : { "first" : "John", "last" : "McCarthy" } },
  4. { "_id": 3, "name": { "first" : "Grace", "last" : "Hopper" } },
  5. { "_id": 4, "firstname": "Ole-Johan", "lastname" : "Dahl" },
  6. ])

Then the following $replaceRoot operation fails because oneof the documents does not have the name field:

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

To avoid the error, you can use $mergeObjects to mergethe name document into some default document; for example:

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

Alternatively, you can skip the documents that are missing the name field byincluding a $match stage to check for existence of thedocument field before passing documents to the $replaceRootstage:

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

Or, you can use $ifNull expression to specify some otherdocument to be root; for example:

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

Examples

$replaceRoot with an Embedded Document Field

A collection named people contains the following documents:

  1. { "_id" : 1, "name" : "Arlene", "age" : 34, "pets" : { "dogs" : 2, "cats" : 1 } }
  2. { "_id" : 2, "name" : "Sam", "age" : 41, "pets" : { "cats" : 1, "fish" : 3 } }
  3. { "_id" : 3, "name" : "Maria", "age" : 25 }

The following operation uses the $replaceRoot stage toreplace each input document with the result of a$mergeObjects operation. The $mergeObjectsexpression merges the specified default document with the petsdocument.

  1. db.people.aggregate( [
  2. { $replaceRoot: { newRoot: { $mergeObjects: [ { dogs: 0, cats: 0, birds: 0, fish: 0 }, "$pets" ] }} }
  3. ] )

The operation returns the following results:

  1. { "dogs" : 2, "cats" : 1, "birds" : 0, "fish" : 0 }
  2. { "dogs" : 0, "cats" : 1, "birds" : 0, "fish" : 3 }
  3. { "dogs" : 0, "cats" : 0, "birds" : 0, "fish" : 0 }

$replaceRoot with a Document Nested in an Array

A collection named students contains the following documents:

  1. db.students.insertMany([
  2. {
  3. "_id" : 1,
  4. "grades" : [
  5. { "test": 1, "grade" : 80, "mean" : 75, "std" : 6 },
  6. { "test": 2, "grade" : 85, "mean" : 90, "std" : 4 },
  7. { "test": 3, "grade" : 95, "mean" : 85, "std" : 6 }
  8. ]
  9. },
  10. {
  11. "_id" : 2,
  12. "grades" : [
  13. { "test": 1, "grade" : 90, "mean" : 75, "std" : 6 },
  14. { "test": 2, "grade" : 87, "mean" : 90, "std" : 3 },
  15. { "test": 3, "grade" : 91, "mean" : 85, "std" : 4 }
  16. ]
  17. }
  18. ])

The following operation promotes the embedded document(s) with thegrade field greater than or equal to 90 to the top level:

  1. db.students.aggregate( [
  2. { $unwind: "$grades" },
  3. { $match: { "grades.grade" : { $gte: 90 } } },
  4. { $replaceRoot: { newRoot: "$grades" } }
  5. ] )

The operation returns the following results:

  1. { "test" : 3, "grade" : 95, "mean" : 85, "std" : 6 }
  2. { "test" : 1, "grade" : 90, "mean" : 75, "std" : 6 }
  3. { "test" : 3, "grade" : 91, "mean" : 85, "std" : 4 }

$replaceRoot with a newly created document

You can also create new documents as part of the$replaceRoot stage and use them to replace all the other fields.

A collection named contacts contains the following documents:

  1. { "_id" : 1, "first_name" : "Gary", "last_name" : "Sheffield", "city" : "New York" }
  2. { "_id" : 2, "first_name" : "Nancy", "last_name" : "Walker", "city" : "Anaheim" }
  3. { "_id" : 3, "first_name" : "Peter", "last_name" : "Sumner", "city" : "Toledo" }

The following operation creates a new document out of thefirst_name and last_name fields.

  1. db.contacts.aggregate( [
  2. {
  3. $replaceRoot: {
  4. newRoot: {
  5. full_name: {
  6. $concat : [ "$first_name", " ", "$last_name" ]
  7. }
  8. }
  9. }
  10. }
  11. ] )

The operation returns the following results:

  1. { "full_name" : "Gary Sheffield" }
  2. { "full_name" : "Nancy Walker" }
  3. { "full_name" : "Peter Sumner" }

$replaceRoot with a New Document Created from $$ROOT and a Default Document

Create a collection named contacts with the following documents:

  1. db.contacts.insert([
  2. { "_id" : 1, name: "Fred", email: "fred@example.net" },
  3. { "_id" : 2, name: "Frank N. Stine", cell: "012-345-9999" },
  4. { "_id" : 3, name: "Gren Dell", home: "987-654-3210", email: "beo@example.net" }
  5. ]);

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

  1. db.contacts.aggregate([
  2. { $replaceRoot: { newRoot: { $mergeObjects: [ { _id: "", name: "", email: "", cell: "", home: "" }, "$$ROOT" ] } } }
  3. ])

The aggregation returns the following documents:

  1. { "_id" : 1, "name" : "Fred", "email" : "fred@example.net", "cell" : "", "home" : "" }
  2. { "_id" : 2, "name" : "Frank N. Stine", "email" : "", "cell" : "012-345-9999", "home" : "" }
  3. { "_id" : 3, "name" : "Gren Dell", "email" : "beo@example.net", "cell" : "", "home" : "987-654-3210" }