Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB set limit to $inc

Tags:

mongodb

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

like image 387
user3261312 Avatar asked Feb 05 '14 20:02

user3261312


People also ask

How do I limit results in MongoDB?

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.

How do I create a capped collection in MongoDB?

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.

How do you use limits in aggregation?

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.

Can MongoDB handle billions of documents?

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.


1 Answers

You can restrict it by a query document:

db.players.update({skills : {$lt : 100}}, {$inc : {skills : 1}})

like image 191
Ori Dar Avatar answered Oct 17 '22 01:10

Ori Dar