Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb Replace a portion of a string [duplicate]

Tags:

mongodb

I have a field

"data" : {
        "user" : "derp",
        "id" : "xHOSTNAME_xderp"

I want to replace all docs with xHOSTNAME_* to yHOSTNAME_. Any idea how to search and replace. I've seen some other posts similarly related but none seemed to work.

like image 273
Todd Vernick Avatar asked Feb 09 '23 01:02

Todd Vernick


1 Answers

db.test1.find().forEach(function(doc) {
    doc.data.id = doc.data.id.replace('xHOSTNAME_', 'yHOSTNAME_');
    db.test1.save(doc);
});
like image 155
Ajay Gupta Avatar answered Feb 23 '23 19:02

Ajay Gupta