$replaceAll
Introduced in 5.0
The $replaceAll operator in Amazon DocumentDB is used to replace all occurrences of a specified string pattern within a field with a new string. This operator can be useful for tasks such as data normalization, text cleaning, and string manipulation.
Parameters
-
input: The field or expression containing the string to be replaced. -
find: The string pattern to search for and replace. -
replacement: The string to replace the matched occurrences with.
Example (MongoDB Shell)
The following example demonstrates how to use the $replaceAll operator in an aggregation pipeline to replace all occurrences of the string "Chocolatier" with "Chocolate Co." in the "brandName" field of a "products" collection.
Create sample documents
db.products.insertMany([ { "_id": 1, "productId": "PROD-0Y9GL0", "brandName": "Gordon's Chocolatier", "category": "CPG", "rating": { "average": 4.8 } }, { "_id": 2, "productId": "PROD-1X2YZ3", "brandName": "Premium Chocolatier", "category": "CPG", "rating": { "average": 4.5 } }, { "_id": 3, "productId": "PROD-Y2E9H5", "name": "Nutrition Co. - Original Corn Flakes Cereal", "category": "Breakfast Cereals", "price": 8.5 } ]);
Query example
db.products.aggregate([ { $addFields: { "brandName": { $replaceAll: { input: "$brandName", find: "Chocolatier", replacement: "Chocolate Co." } } } } ])
Output
[
{
_id: 1,
productId: 'PROD-0Y9GL0',
brandName: "Gordon's Chocolate Co.",
category: 'CPG',
rating: { average: 4.8 }
},
{
_id: 2,
productId: 'PROD-1X2YZ3',
brandName: 'Premium Chocolate Co.',
category: 'CPG',
rating: { average: 4.5 }
},
{
_id: 3,
productId: 'PROD-Y2E9H5',
name: 'Nutrition Co. - Original Corn Flakes Cereal',
category: 'Breakfast Cereals',
price: 8.5,
brandName: null
}
]
Code examples
To view a code example for using the $replaceAll command, choose the tab for the language that you want to use: