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
Post a Comment