search and replace string mongodb code example
Example 1: mongodb replace string
db.mycollection.find({}).forEach((doc, index) => {
doc.myStr = doc.myStr.replace('foo', 'bar');
db.mycollection.save(doc);
});
Example 2: mongodb replace document
db.collection.replaceOne(
<filter>,
<replacement>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
hint: <document|string> // Available starting in 4.2.1
}
)