javascript - 如果满足条件,更新收藏并发送电子邮件 || Mongo Nodejs
问题描述
我有以下情况 - 我想遍历 db 中的每个元素,并且:
- 如果元素的
bumped
字段设置为false
- 并且创建日期小于 30 天前
然后:
- 设置
bumped
为true
- 发送邮件给用户!
我的做法:
User.updateMany(
{
bumped: false,
creationDate: {
$gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
},
},
{
$set: {
bumped: true,
},
},
(err, res) => {
//
// What is "res" here? <====== question
},
);
我的问题-res
回调函数中的参数是什么?
问题2:是否只会为这些满足条件的元素触发回调?
非常感谢!
解决方案
updateMany
函数不返回更新的文档。它只返回更新的文档数。
所以在这里你唯一能做的就是首先找到所有的文档并一个一个地迭代,然后可以调用发送邮件功能。
const users = await User.find({
"bumped": false,
"creationDate": {
"$gte": new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
}
})
const promises = users.map(async(user) => {
await User.updateOne({ _id: user._id }, { $set: { bumped: true }})
// Here you can write your send mail function
})
await Promise.all(promises)
推荐阅读
- python - udp客户端python获取时间
- image - 不允许使用无 IPTC 的图片格式
- python - 如何使用 scipy 在 python 中绘制 fft 并修改频率范围,使其显示中心的两个峰值频率?
- c# - 从文件路径中的文件名中获取第一个字符
- google-cloud-build - 是否可以使用 Google CloudBuild 将图像推送到 AWS ECR?
- python - 层sequential_109的输入0与层不兼容:预期ndim=3,发现ndim=2。收到的完整形状:(1, 60)
- javascript - 如何刷新 Twilio Conversations API 的令牌?
- encryption - 查找 ECC 的公钥
- swiftui - 如何在 SwiftUI 中为 WKWebView 创建浏览器选项卡
- linux - 调试:我没有将反汇编调用者中的回调视为 callq 或内联 asm (Linux x86_64)