仅当值大于 0 时$inc (Node.js mongodb)



您好,我有以下更新查询

this.dbService.getCollection(DBCollections.CHANNEL)
.updateMany(
{
'members.skipRounds': { $exists: true },
_id: channelId,
},
{ $inc: { 'members.$[].skipRounds': -1 } },
).catch(err => console.log(err));

如何使skipRounds的值仅在当前值大于0时才减少-1?

这是我的频道集合的样子

Channels [
{
_id:1
users:[
{
_id:1
skipRounds:1
},
{
_id:2
skipRounds:3
},       
]
},
{
_id:2
users:[
{
_id:1
skipRounds:3
},
{
_id:2
skipRounds:5
},       
]
},   
] 

请检查这个它可能会对您有所帮助。

this.dbService.getCollection(DBCollections.CHANNEL).updateMany(
{
'users.skipRounds': { $exists: true },
},
{ $inc: { 'users.$[element].skipRounds': -1 } },
{ arrayFilters: [ { "element.skipRounds": { $gt: 0 } } ]}
).catch(err => console.log(err));

最新更新