I'm trying to decrement a field in a document and set a default value if it does not exist. My query below:
usersCollection.findOneAndUpdate(
{_id: id},
{
$inc: {quota: -1},
$setOnInsert: {quota: 100}
},
{
upsert: true,
returnOriginal: false
}
)
But this query gives the following error:
Updating the path 'quota' would create a conflict at 'quota'
If I don't use the $setOnInsert: {quota: 100}
then it works but starts decrementing from 0 which I don't want. My goal is to set a default value (100) in this case and decrement on each query.
Is this possible in 1 query ? I have searched around but didn't find something that could help.