1

So I'm currently trying to perform this operation

return this.model.findByIdAndUpdate(id, { $push: { certifiedBy: certifier } }, { $inc: {score: 1}}, { new: true })

The issue here is that score will grow without limit, I would like to prevent this and make it so when this is happening it cannot increment if score <= 5 but still add certifier into my certifiedBy array.

Can it be done with mongoose directly or do I have to get the object first check if it over 5 and call a different query in that case ?

Thanks

girard_s
  • 119
  • 1
  • 8

1 Answers1

0

You can't change the $inc behaviour but you can do a checkpoint to stop it before 5

return this.model.findOneAndUpdate({
    _id: id,
    score: {
        $lte: 5
    }
}, {
        $push: { certifiedBy: certifier },
        $inc: { score: 1 }
    },
    {
        new: true
    })
Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54