数据库 member 表字段格式举例:
{
"_id" : ObjectId("xxxxxx"),
...
"socials" : [
{
"channel" : "wechat-channelId1",
"openId" : "wechat是是openId1",
},
{
"channel" : "wechat-channelId2",
"openId" : "wechat-openId2",
},
{
"channel" : "weapp-channelId3",
"openId" : "weapp-openId3",
},
{
"channel" : "weibo-channelId4",
"openId" : "weibo-openId4",
},
],
...
}
socials.openId 字段中可能有脏数据,要把“是”替换为“1”
脚本
var count = 0;
db.member.find({
"socials.openId": /^.*是.*$/,
}).snapshot().forEach(function(member) {
var newSocials = []
member.socials.forEach(function(social) {
var oldOpenId = social.openId;
var tmp = social.openId;
if (tmp != null){
tmp = tmp.replace(/是/g,"1");
if (tmp != oldOpenId) {
print('memberId: ' + member._id + ', oldOpenId: ' + oldOpenId + ', newOpenId: ' + tmp);
}
}
social.openId = tmp;
newSocials.push(social);
})
member.socials = newSocials;
db.member.save(member);
count++;
});
print('update done with count: ' + count);