使用mongoose在mongodb集合中的字符串前添加电子邮件地址 [英] prepend email addresses with string in mongodb collection using mongoose
本文介绍了使用mongoose在mongodb集合中的字符串前添加电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的收藏有一个名为email的列,其中包含电子邮件地址.现在,我需要更新所有这些电子邮件地址,在电子邮件末尾(@之前)附加_01.基本上,如果电子邮件为ipman@gmail.com
,则应将其转换为ipman_01@gmail.com
.我知道我们可以使用updateMany
使用猫鼬更新多个文档,但是要实现我所需要的,我相信我也需要使用某种正则表达式吗?
My collection has a column named email which contains email address. Now I need to update all these email addresses, appending _01 at the end of the email (before @). Basically if the email is ipman@gmail.com
, it should be converted to ipman_01@gmail.com
. I know we can use updateMany
to update multiple documents using mongoose, but to achieve what I need, I believe I need to use some kind of regex too?
推荐答案
请尝试以下操作:
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)
参考: .bulkWrite()
这篇关于使用mongoose在mongodb集合中的字符串前添加电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文