替换所有 Mongodb 文档的文档字段中的多个字符串
Replace multiple strings in document field for all Mongodb documents
我有这个代码:
db.Collection.find().forEach(function(doc) {
doc.content = doc.content.replace('old', 'new');
db.Collection.save(doc);
});
它工作正常,但它只替换文档字段中的第一个字符串。我的目标是替换所有字符串:
content: 'old old old'
到
content:'new new new'
发布几秒钟后,我用正则表达式解决了问题:
db.Collection.find().forEach(function(doc) {
doc.content = doc.content.replace(/\old/g, 'new');
db.Collection.save(doc);
});
我有这个代码:
db.Collection.find().forEach(function(doc) {
doc.content = doc.content.replace('old', 'new');
db.Collection.save(doc);
});
它工作正常,但它只替换文档字段中的第一个字符串。我的目标是替换所有字符串:
content: 'old old old'
到
content:'new new new'
发布几秒钟后,我用正则表达式解决了问题:
db.Collection.find().forEach(function(doc) {
doc.content = doc.content.replace(/\old/g, 'new');
db.Collection.save(doc);
});