2016-08-24 71 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); 
} 
相关问题