2016-08-24 70 views
-1

我有一個包含具有分隔符「|」的文檔的集合。如何將mongodb中的字符替換爲集合中的另一個字符?

{

「_id」:物件( 「57bbe4342a00d122b0075fbb」),

「phone_search」: 「9255958588 | 9138115601 | 9034223813」,

「地址」:「中央複雜的市場|羅塔克路|索尼帕特|羅塔克 路 - 131001 | Sonepat |哈里亞納邦」,

「national_catidlineage_search」: 「/ 10255012/|/10406930 /」,

「區域」: 「羅塔克路」,

}

有MongoDB中的任何命令,它可以取代所有 「|」 s的 「」 S集合中的所有文件?

+0

總是可以發佈您的問題之前先搜索。我已經在前兩個谷歌搜索結果中找到了兩個類似的帖子。請參閱[this](http://stackoverflow.com/questions/10042450/how-to-replace-string-in-all-documents-in-mongo)和[this](http://stackoverflow.com/questions/ 12589792 /如何更換的子串功能於MongoDB的文檔)... –

回答

0

這個問題在這裏回答How to replace string in all documents in Mongo

// Change 'collection' name for yours in db.collection.find and db.collection.update: 
var cursor = db.collection.find(); 
while (cursor.hasNext()) { 
    var x = cursor.next(); 
    print("Before: "+x['phone_search']); 
    x['phone_search'] = x['phone_search'].replace('|', ','); 
    print("After: "+x['phone_search']); 
    // Uncomment next line to persist: 
    // db.collection.update({_id : x._id}, x); 
} 
相關問題