$replaceWith is a MongoDB aggregation stage that replaces the whole input document with an expression result. It does the same job as $replaceRoot, with a shorter form.
Remember
$replaceWith = $replaceRoot, shorter syntax
{ $replaceWith: <document expression> }
Same as:
{ $replaceRoot: { newRoot: <document expression> } }
expression must resolve to a document
(missing / non-document → error)
Common:
{ $replaceWith: "$name" }
{ $replaceWith: { full_name: ... } }
vs $project → swap entire root vs reshape fields
Choose the replacement document, ensure it always exists, then use the short stage form.
Decide the new document—a field path like "$name", or a built object.
Optional: $match so every document has that nested field.
Add { $replaceWith: <expression> }.
If you still need _id or defaults, merge with $mergeObjects or guard with $ifNull.
Piece
Required?
Role
Expression
Yes
Must resolve to a document
Field path
Common
Promote an embedded object (e.g. "$name")
Built object
Optional
Create a fresh document shape
Pseudocode
Pseudocode
for each document:
next = evaluate(expression)
if next is missing or not a document:
error
emit next // old fields (including _id) are gone
// same result as $replaceRoot { newRoot: expression }
Reference
Cheat Sheet
Keep these facts handy while swapping document roots.
A string, number, or array is not valid. Promote an object field, or build { ... }.
_id
Original _id is discarded
Unless you copy it into the replacement document, the output has no outer _id (or uses one from the nested doc).
alias
Not a different stage behavior
$replaceWith and $replaceRoot are aliases in effect. Switching forms does not change results.
Analysis
Performance Notes
Approach
Cost
Best for
Promote one nested field
Per document
Flatten embedded objects for APIs
$match then replace
Filter first
Skip docs that would error
$unwind + replace
More docs
One root per array element
Cost matches $replaceRoot. Prefer $match first when some documents lack the nested field. Pair with $unwind only when you need one document per array element.
Remember
Key Takeaways
Role: replace the whole document with one expression result.
Shorthand: same as $replaceRoot without the newRoot wrapper.
Must be a document: missing or non-document values error.
vs $project: swap the root vs reshape fields on the current doc.
One line:$replaceWith is the short way to say “make this expression the whole document.”
Frequently Asked Questions
$replaceWith replaces the entire input document with a document from a single expression. Behavior matches $replaceRoot—promote a nested object or emit a newly built document.
Same operation, shorter form. $replaceRoot uses { newRoot: <expression> }. $replaceWith is { $replaceWith: <expression> }. Choose the form that reads clearer.
The old document—including _id—is discarded unless you put _id in the replacement expression.
The stage errors. Guard with $match, $ifNull, or $mergeObjects with defaults before or inside $replaceWith.
MongoDB 4.2 added $replaceWith as a shorthand for $replaceRoot. Both remain supported.
$project reshapes fields on the current document. $replaceWith discards the outer document and substitutes the expression result as the whole document.