Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrease the value of a database entry

Tags:

mongodb

I have a nodeJS system where a student enrolls for a batch, and so i have a field in the mongodb as batchSize which holds maximum number of students in one batch, student can enroll on a batch and batchSize should reduce its value

how to write a query to do the same?

like image 896
Syed Faizan Avatar asked Feb 23 '26 03:02

Syed Faizan


1 Answers

Batch.update({ _id: data.batch._id }, { $push: { 'students': data.student }, $inc: { currentlySubscribed: 1 } }

Batch.update({ _id: data.batch._id }, { $pull: { 'students': { 'email': data.student.email } }, $inc: { currentlySubscribed: -1 } }
like image 126
maxboo Avatar answered Feb 25 '26 17:02

maxboo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!