MongoDB set limit to $inc
mongodb
Solution
You can restrict it by a query document:
`db.players.update({skills : {$lt : 100}}, {$inc : {skills : 1}})`
Problem
I have database of sports players which contains their "skills". For example football player in database has 14 skills. Each day I simulate training of them so trained skill that day increases by some amount. My collection has over 1.5 million records, so I do bulk update. The problem is that using $inc, it can set value over limit, in my case the skill limit is 100. How could I set limit to field, so it won't be updated more if it has exceeded its limit? I have also tried to do skills "fixing" after updating, I mean after training simulation is done, I run script which iterates all players and if it has a field of exceeded value, I make update query to fix it, but making 1.5 million separate queries is kill for the server, so are there any other ways of doing it? Edit: increased value has some random values in calculations, so it is not the same for all players. But the value can be from 0 to 1, rounded to 2nd digit,for example it can be 0.1, 0.2, 0.3.... 1 . So I group players with this value and trained skill and make update to database using operator $in, which contains players ids who train the same skill and their training value is the same