$replaceWith
New from version 8.0
Not supported by Elastic cluster.
The $replaceWith aggregation stage in Amazon DocumentDB is used to replace the input document with a new document. All existing fields on the input document, including the _id field, are replaced by the new document. $replaceWith is commonly used to flatten documents, or promote an embedded document to the top level.
Parameters
-
<replacement>(required): The new document that will replace the existing document.
Example (MongoDB Shell)
The following example demonstrates how to use the $replaceWith operator to replace an existing document in an Amazon DocumentDB collection.
Create sample documents
db.restaurants.insertMany([ { "restaurantId": "REST-0Y9GL0", "name": "Biryani Adda", "cuisine": "Indian", "ratings": [ 3, 4, 3, 2, 2, 4, 1, 5, 5, 5 ] }, { "restaurantId": "REST-8L2PX9", "name": "The Burger Spot", "cuisine": "American", "ratings": [ 2, 3, 4, 5, 3, 1, 1, 2, 4 ] } ]);
Query example
db.restaurants.aggregate([ { $replaceWith: { name: "$name", cuisine: "$cuisine", rating: { $avg: "$ratings" } } } ]);
Output
[
{
name: 'Biryani Adda',
cuisine: 'Indian',
rating: 3.4
},
{
name: 'The Burger Spot',
cuisine: 'American',
rating: 2.7777777777777777
}
]
Code examples
To view a code example for using the $replaceWith command, choose the tab for the language that you want to use: