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
In MongoDB, you can use the limit() method to specify a maximum number of documents for a cursor to return. When you query a collection using the db. collection. find() method, you can append limit() to specify the limit.
To create a capped collection, we use the normal createCollection command but with capped option as true and specifying the maximum size of collection in bytes. This code would convert our existing collection posts to a capped collection.
Answer: $skip before $limit In aggregate, $limit limits the number of documents sent to the next aggregation state, and $skip skips the first N documents, so if $skip is after $limit and $skip >= $limit, you won't get any results.
Mongo can easily handle billions of documents and can have billions of documents in the one collection but remember that the maximum document size is 16mb. There are many folk with billions of documents in MongoDB and there's lots of discussions about it on the MongoDB Google User Group.
You can restrict it by a query document:
db.players.update({skills : {$lt : 100}}, {$inc : {skills : 1}})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With