4

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.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Todd Vernick
  • 41
  • 1
  • 2
  • Wrong link: Same basic question but answers are more useful: http://stackoverflow.com/questions/3788256/mongodb-updating-documents-using-data-from-the-same-document/3792958#3792958 – Blakes Seven Sep 11 '15 at 01:08

1 Answers1

8
db.test1.find().forEach(function(doc) {
    doc.data.id = doc.data.id.replace('xHOSTNAME_', 'yHOSTNAME_');
    db.test1.save(doc);
});
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
Ajay Gupta
  • 3,192
  • 1
  • 22
  • 30