【发布时间】:2021-10-28 21:43:18
【问题描述】:
如何更新 mongoose 上的许多文档并返回这些更新的文档,以便我可以将更新的文档传递给我的代码上的不同服务?这看起来很简单,但我对如何实现它感到困惑
在我当前的代码中,我只是使用 updateMany 批量更新文档,但正如 mongo 文档所说,返回的 writeConcern 只是更新的文档数 {n: 0 } 不是实际文件。
当前代码:
const checkAndUpdateSubscription = () => {
const filter = {
"payments.0.stripeSubscriptionEndDate": { $lte: today },
hasPaid: true,
};
const update = { $set: { hasPaid: false, isSubscriptionNew: 0 } };
const options = { new: true, useFindAndModify: false };
return new Promise((resolve, reject) => {
ModelModel.updateMany(filter, update, options)
.then((response) => {
console.log('response inside checkAndUpdateSubscription', response)
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};
我想把它改成类似于下面我的伪代码的东西。
我想做的事:
const checkAndUpdateSubscription = () => {
const filter = {
"payments.0.stripeSubscriptionEndDate": { $lte: today },
hasPaid: true,
};
const update = { $set: { hasPaid: false, isSubscriptionNew: 0 } };
const options = { new: true, useFindAndModify: false };
return new Promise((resolve, reject) => {
// 1. ModelModel.find where stripeSubscriptionEndDate $lte than today ..etc
// 2. Update the document(s)
// 3. Return the updated document(s)
(//4.) .then(updatedModel => sendUpdateModelToOutsideService(updatedModel))
});
};
我不知道在这个问题的上下文中这是否有必要,但checkAndUpdateSubscription 方法是一个函数,它在我的数据库中每 1 分钟为我的所有用户运行一次 (# ~thousands)
【问题讨论】: