-1

I have a collection which contains documents having delimiter "|".

{

"_id" : ObjectId("57bbe4342a00d122b0075fbb"),

"phone_search" : "9255958588|9138115601|9034223813",

"address" : "Central Complex Market|Rohtak Road|Sonipat|Rohtak Road-131001|Sonepat|HARYANA",

"national_catidlineage_search" : "/10255012/|/10406930/",

"area" : "Rohtak Road",

}

Is there any command in mongodb which can replace all "|"s with ","s for all the documents in the collection?

Anubhav
  • 3
  • 4
  • Always search first before posting your question. There are already two similar post i've found in first two google results. See [this](http://stackoverflow.com/questions/10042450/how-to-replace-string-in-all-documents-in-mongo) and [this](http://stackoverflow.com/questions/12589792/how-to-replace-substring-in-mongodb-document)... – d.coder Aug 24 '16 at 06:58

1 Answers1

0

This question was answered here How to replace string in all documents in Mongo

// Change 'collection' name for yours in db.collection.find and db.collection.update:
var cursor = db.collection.find();
while (cursor.hasNext()) {
  var x = cursor.next();
  print("Before: "+x['phone_search']);
  x['phone_search'] = x['phone_search'].replace('|', ',');
  print("After: "+x['phone_search']);
  // Uncomment next line to persist:
  // db.collection.update({_id : x._id}, x);
}
aabilio
  • 1,697
  • 12
  • 19