Mongodb Replace a word from the string -


so have mongodb document have field this

image : http://static14.com/p/inc.5-black-sandals-5131-2713231-7-zoom.jpg 

i want replace zoom in string other text such :

   image : http://static14.com/p/inc.5-black-sandals-5131-2713231-7-product2.jpg 

is possible that??

you use mongo's foreach() cursor method atomic update $set operator :

db.collection.find({}).snapshot().foreach(function(doc) {     var updated_url = doc.image.replace('zoom', 'product2');     db.collection.update(         {"_id": doc._id},          { "$set": { "image": updated_url } }     ); }); 

given large collection update, speed things little bit bulkwrite , restructure update operations sent in bulk as:

var ops = []; db.collection.find({}).snapshot().foreach(function(doc) {     ops.push({         "updateone": {             "filter": { "_id": doc._id },             "update": { "$set": { "image": doc.image.replace('zoom', 'product2') } }         }     });      if ( ops.length === 500 ) {         db.collection.bulkwrite(ops);         ops = [];     } })  if ( ops.length > 0 )       db.collection.bulkwrite(ops); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -