$replaceOne - Amazon DocumentDB

$replaceOne

Introduced in 5.0

The $replaceOne operator in Amazon DocumentDB is a string expression operator used within aggregation pipelines to replace the first occurrence of a specified substring within a string with a replacement string. This operator is case-sensitive and only replaces the first match found.

Parameters

  • input: The string (field) on which to perform the find.

  • find: The string to search for within the input.

  • replacement: The string to replace the first occurrence of the find in the input(field).

Example (MongoDB Shell)

The following example demonstrates how to use the $replaceOne operator within an aggregation pipeline to replace substrings in product names.

Create sample documents

db.products.insertMany([ { "_id":1, "productId": "PROD-0Y9GL0", "name": "Gordon's Extra Creamy Milk Chocolate - Pack of 4", "category": "Confectionery", "price": 24.99 }, { "_id":2, "productId": "PROD-Y2E9H5", "name": "Nutrition Co. - Original Corn Flakes Cereal", "category": "Breakfast Cereals", "price": 8.50 }, { "_id":3, "productId": "PROD-Z3F8K2", "name": "Gordon's Dark Chocolate (90% Cocoa) Pack - Pack of 4", "category": "Confectionery", "price": 28.99 } ]);

Aggregation example

db.products.aggregate([ { $addFields: { standardizedName: { $replaceOne: { input: "$name", find: "Pack", replacement: "Package" } } } } ]);

Output

The output shows that only the first occurrence of "Pack" in each product name was replaced with "Package".

[ { _id: 1, productId: 'PROD-0Y9GL0', name: "Gordon's Extra Creamy Milk Chocolate - Pack of 4", category: 'Confectionery', price: 24.99, standardizedName: "Gordon's Extra Creamy Milk Chocolate - Package of 4" }, { _id: 2, productId: 'PROD-Y2E9H5', name: 'Nutrition Co. - Original Corn Flakes Cereal', category: 'Breakfast Cereals', price: 8.5, standardizedName: 'Nutrition Co. - Original Corn Flakes Cereal' }, { _id: 3, productId: 'PROD-Z3F8K2', name: "Gordon's Dark Chocolate (90% Cocoa) Pack - Pack of 4", category: 'Confectionery', price: 28.99, standardizedName: "Gordon's Dark Chocolate (90% Cocoa) Package - Pack of 4" }

Code examples

To view a code example for using the $replaceOne command, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function replaceOne() { 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('products'); const pipeline = [ { $addFields: { standardizedName: { $replaceOne: { input: '$name', find: 'Pack', replacement: 'Package' } } } } ]; const result = await collection.aggregate(pipeline).toArray(); console.log(result); await client.close(); } replaceOne();
Python
from pymongo import MongoClient def replaceOne(): 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['products'] pipeline = [ { '$addFields': { 'standardizedName': { '$replaceOne': { 'input': '$name', 'find': 'Pack', 'replacement': 'Package' } } } } ] result = list(collection.aggregate(pipeline)) print(result) client.close() replaceOne()