String replacement in MongoDB? -


i have collection has number of documents title contains encoded & characters (&) , convert these back.

i'm wondering if possible string replacement on value based on it's original value.

for example document below:

{    title: "this & that" } 

is possible replace & normal update statement? tried this:

db.questions.update({title: /&/}, {title: title.replace(/&/g, "&")}); 

but threw following error: referenceerror: title not defined

can done using update statment? if not, best way find/foreach/update on each document?

i'm wondering if possible string replacement on value based on it's original value....can done using update statment?

currently, not possible update field's value in mongodb accessing,modifying original value in single update statement.

but threw following error: referenceerror: title not defined

this error occurs, because mongodb not see title field of document getting updated in scope.

if not, best way find/foreach/update on each document?

yes. need way.

db.collection.find({"title":/&amp/}).foreach(function(doc){ db.collection.update({"_id":doc._id},                      {$set:{"title": doc.title.replace(/&/g, "&")}}) }) 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -