基于条件的更新
await products.updateOne(
{
$and: [
{ name: { $eq: name } },
{ $expr: { $lt: ["$remaining", "$capacity"] } },
],
},
{ $inc: { remaining: 1 } },
{ returnOriginal: false }
);
而不是像So {$ expr:{$ lt:[“ $剩余”,“ $ apcation”}}}
那样,而不是像SO {$ expr:{$ lt:{$ lt:[$ lt:争论?
这样做的原因是,我希望返回的matchCount
在匹配name
的情况下返回1。
await products.updateOne(
{
$and: [
{ name: { $eq: name } },
{ $expr: { $lt: ["$remaining", "$capacity"] } },
],
},
{ $inc: { remaining: 1 } },
{ returnOriginal: false }
);
Instead of having the condition in the query like so { $expr: { $lt: ["$remaining", "$capacity"] } }
, is there a way to include this condition in the update argument?
The reason for this is so that I want the returned matchCount
to return 1 if the name
is matched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果您使用聚合更新使用Mongo 4.2+,则可以做到这一点。
态
Yes, you can do that if you use mongo 4.2+ using aggregate update.
playground