mongodbmongodb-.net-drivermongo-shellmongodb-update

How to replace substring in mongodb document


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?


Solution

  • db.media.find({mediaContainer:"ContainerS3"}).forEach(function(e,i) {
        e.url=e.url.replace("//a.n.com","//b.n.com");
        db.media.save(e);
    });