2014-12-01 2 views
0

title& 문자 (&)가 인코딩되어있는 문서가 포함 된 모음이 있으며이를 다시 변환하고 싶습니다.MongoDB에서 문자열 바꾸기?

원래 값을 기준으로 값을 문자열로 대체 할 수 있는지 궁금합니다.

아래의 문서와 예를 들어

{ 
    title: "this & that" 
} 

그것이 가능 정상적인 업데이트 문으로 &를 대체? 나는이 같은 시도 :

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

을하지만, 다음과 같은 오류 던졌다 :이 업데이트 한 Statment를 사용 ReferenceError: title is not defined

을 할 수 있습니까? 그렇지 않다면 각 문서에서 찾기/foreach/update를 수행하는 가장 좋은 방법입니까?

답변

1

I'm wondering if it is possible to do a string replacement on a value based on it's original value....Can it be done using an update statment?

현재는 가 하나의 update 문에 원래의 값을 수정, 에 액세스하여 MongoDB의에서 필드의 값을 업데이트 할 수없는입니다. MongoDB가이 범위에 업데이트지고 문서의 title 필드가 표시되지 않기 때문에

But it threw the following error: ReferenceError: title is not defined

이 오류가 발생합니다.

If not, is the best way just to do a find/foreach/update on each document?

예. 그런 식으로해야합니다.

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