使用mongoose在mongodb集合中使用字符串前置电子邮件地址
我的收藏有一个名为email的列,其中包含电子邮件地址。现在我需要更新所有这些电子邮件地址,并在电子邮件末尾(在@之前)添加_01。如果电子邮件是使用mongoose在mongodb集合中使用字符串前置电子邮件地址,mongodb,mongoose,Mongodb,Mongoose,我的收藏有一个名为email的列,其中包含电子邮件地址。现在我需要更新所有这些电子邮件地址,并在电子邮件末尾(在@之前)添加_01。如果电子邮件是ipman@gmail.com,应将其转换为ipman_01@gmail.com。我知道我们可以使用updateMany使用mongoose更新多个文档,但为了实现我的需要,我相信我也需要使用某种正则表达式?请尝试以下方法: let emails = ['ipman@gmail.com', 'ipman2@gmail.com'] let bulkAr
ipman@gmail.com
,应将其转换为ipman_01@gmail.com
。我知道我们可以使用updateMany
使用mongoose更新多个文档,但为了实现我的需要,我相信我也需要使用某种正则表达式?请尝试以下方法:
let emails = ['ipman@gmail.com', 'ipman2@gmail.com']
let bulkArr = [];
for (const i of emails) {
let newEmail = i.split("@");
newEmail = newEmail[0] + '_01' + '@' + newEmail[1]
bulkArr.push({
updateOne: {
"filter": { email : i },
"update": { '$set': { email : newEmail } }
}
})
}
let updateResult = await MongooseModel.bulkWrite( bulkArr, { ordered : false })
/** Passing { ordered : false } to make sure update op doesn't fail if updating one document in the middle fails, Also bulkWrite returns write result check documentation for more info */
console.log('matchedCount ::', updateResult.matchedCount, 'modifiedCount ::', updateResult.modifiedCount)
Ref:您的mongoDB版本是什么?@srinivasy它是4.0.7您的数据集有多大?集合大小只有大约1.6k,但我想将这些应用到集合中选定的数据集(只有63条记录),我将使用Arraysorry中的find。我陷入了另一项任务。今天我将测试您的代码,并相应地通知您k。谢谢你的帮助