首页 > 解决方案 > MongoDB - 如何通过字符串操作更新值

问题描述

id在数据库中有字段,值在模式中entity_id_aaaa-bbbb-cccc-dddd。我需要用下划线更改模式,并将所有现有文档中的全部替换-_下划线(最终结果应该是entity_id_aaaa_bbbb_cccc_dddd),MongoDb 中是否有任何更新查询可以帮助我?

标签: mongodb

解决方案


db.collection.find({id:id}).forEach(function(document) {
    document.id = document.id.replace('entity_id_aaaa-bbbb-cccc-dddd', 'entity_id_aaaa_bbbb_cccc_dddd');
    db.collection.save(document);
});

推荐阅读