- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $replaceWith (aggregation)
$replaceWith (aggregation)¶
On this page
Definition¶
-
$replaceWith
¶ New in version 4.2.
Replaces the input document with the specified document. The operation replaces all existing fields in the input document, including the
_id
field. With$replaceWith
, you can promote an embedded document to the top-level. You can also specify a new document as the replacement.The
$replaceWith
is an alias for$replaceRoot
.The
$replaceWith
stage has the following form:The replacement document can be any valid expression that resolves to a document. For more information on expressions, see Expressions.
Behavior¶
If the <replacementDocument>
is not a document,
$replaceWith
errors and fails.
If the <replacementDocument>
resolves to a missing document (i.e.
the document does not exist), $replaceWith
errors and
fails. For example, create a collection with the following
documents:
Then the following $replaceWith
operation fails because one
of the document does not have the name
field:
To avoid the error, you can use $mergeObjects
to merge
the name
document with some default document; for example:
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 $replaceWith
stage:
Or, you can use $ifNull
expression to specify some other
document to be root; for example:
Examples¶
$replaceWith
an Embedded Document Field¶
Create a collection named people
with the following documents:
The following operation uses the $replaceWith
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.
The operation returns the following results:
$replaceWith
a Document Nested in an Array¶
A collection named students
contains the following documents:
The following operation promotes the embedded document(s) with the
grade
field greater than or equal to 90
to the top level:
The operation returns the following results:
$replaceWith
a Newly Created Document¶
Example 1¶
An example collection sales
is populated with the following
documents:
Assume that for reporting purposes, you want to calculate for each
completed sale, the total amount as of the current report run time. The
following operation finds all the sales with status C
and creates
new documents using the $replaceWith
stage. The
$replaceWith
calculates the total amount as well as uses
the variable NOW
to get the current time.
The operation returns the following documents:
Example 2¶
An example collection reportedsales
is populated with the
reported sales information by quarter and regions:
Assume that for reporting purposes, you want to view the reported sales data by quarter; e.g.
To view the data grouped by quarter, you can use the following aggregation pipeline:
- First stage:
The
$addFields
stage adds a newobj
document field that defines the keyk
as the region value and the valuev
as the quantity for that region. For example:- Second stage:
The
$group
stage groups by the quarter and uses$push
to accumulate theobj
fields into a newitems
array field. For example:- Third stage:
The
$project
stage uses$concatArrays
to create a new arrayitems2
that includes the_id
info and the elements from theitems
array:- Fourth stage:
The
$replaceWith
uses the$arrayToObject
to convert theitems2
into a document, using the specified keyk
and valuev
pairs and outputs that document to the next stage. For example:
The aggregation returns the following document:
$replaceWith
a New Document Created from $$ROOT
and a Default Document¶
Create a collection named contacts
with the following documents:
The following operation uses $replaceWith
with
$mergeObjects
to output current documents with default
values for missing fields:
The aggregation returns the following documents: