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":/&/}).foreach(function(doc){ db.collection.update({"_id":doc._id}, {$set:{"title": doc.title.replace(/&/g, "&")}}) })
Comments
Post a Comment