MongoDB-如何通过字符串操作更新值

时间:2019-10-14 10:39:13

标签: mongodb

我在数据库中有字段Map<String, sObjectType> objects = Schema.getGlobalDescribe(); for(String objName : objects.keySet()){ system.debug('=======' + objName + '=========\n Fields: '); sObjectType o = objects.get(objName); DescribeSobjectResult oDRes = o.getDescribe(); Map<String, Schema.SObjectField> fields = dResult.fields.getMap(); for(Schema.SObjectField f : fields.values()){ DescribeFieldResult fDRes = f.getDescribe(); system.debug('name: ' + fDRes.getName() + ' | type: ' + fDRes.getType()); } } ,其值采用模式id。我需要使用下划线更改模式,并将所有现有文档中的所有entity_id_aaaa-bbbb-cccc-dddd替换为-的下划线(最终结果应为_),MongoDb中是否有任何更新查询会帮助我吗?

1 个答案:

答案 0 :(得分:0)

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);
});