$replaceWith - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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:

Node.js
const { MongoClient } = require('mongodb'); async function replaceDoc() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('restaurants'); const result = await collection.aggregate([ { $replaceWith: { name: "description_index", cuisine: 2, rating: { $avg: "$ratings" } } } ]).toArray(); console.log(result); client.close(); } replaceDoc();
Python
from pymongo import MongoClient def replace_document(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test collection = db.restaurants result = list(collection.aggregate([ { '$replaceWith': { 'name': "$name", 'cuisine': "$cuisine", 'rating': { '$avg': "$ratings"} } } ])) print(result) client.close() replace_document()