I have a lot of mongodb documents in a collection of the form:
{ .... "URL":"www.abc.com/helloWorldt/..." ..... }
I want to replace helloWorldt
with helloWorld
to get:
{ .... "URL":"www.abc.com/helloWorld/..." ..... }
How can I achieve this for all documents in my collection?
MongoDB gives the functionality to search a pattern in a string through a query by writing a regular expression. The regular expression capabilities are used for pattern matching strings in queries and for that, we use the $regex operator. Syntax: db.
db.media.find({mediaContainer:"ContainerS3"}).forEach(function(e,i) { e.url=e.url.replace("//a.n.com","//b.n.com"); db.media.save(e); });
Nowadays,
Mongo 4.2
, db.collection.updateMany
(alias of db.collection.update
) can accept an aggregation pipeline, finally allowing the update of a field based on its own value.Mongo 4.4
, the new aggregation operator $replaceOne
makes it very easy to replace part of a string.// { URL: "www.abc.com/helloWorldt/..." } // { URL: "www.abc.com/HelloWo/..." } db.collection.updateMany( { URL: { $regex: /helloWorldt/ } }, [{ $set: { URL: { $replaceOne: { input: "$URL", find: "helloWorldt", replacement: "helloWorld" } }} }] ) // { URL: "www.abc.com/helloWorld/..." } // { URL: "www.abc.com/HelloWo/..." }
{ URL: { $regex: /helloWorldt/ } }
) is the match query, filtering which documents to update (the ones containing "helloWorldt"
) and is just there to make the query faster.$set: { URL: {...
) is the update aggregation pipeline (note the squared brackets signifying the use of an aggregation pipeline): $set
is a new aggregation operator (Mongo 4.2
) which in this case replaces the value of a field.$replaceOne
operator. Note how URL
is modified directly based on the its own value ($URL
).Before Mongo 4.4
and starting Mongo 4.2
, due to the lack of a proper string $replace
operator, we have to use a bancal mix of $concat
and $split
:
db.collection.updateMany( { URL: { $regex: "/helloWorldt/" } }, [{ $set: { URL: { $concat: [ { $arrayElemAt: [ { $split: [ "$URL", "/helloWorldt/" ] }, 0 ] }, "/helloWorld/", { $arrayElemAt: [ { $split: [ "$URL", "/helloWorldt/" ] }, 1 ] } ] }} }] )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With