Re:MongoDB, count() and the big O
by kevin1024 on Thursday November 24, 2011 @12:09AM
(#3273165)
You can combine limit() with count by passing true to the count function. So if you only care about counts greater than, say, 10000, you can do db.collection.find({criteria:'123}).limit(10000).count(true) and you will only get back a max of 10000. This puts an upper bound on waiting for some large counts to come back.
[ Parent
| on HN ]
That's awfully handy. It unfortunately doesn't solve the pagination problem, though. :/
Re:MongoDB, count() and the big O
by josh_cutler on Thursday November 24, 2011 @04:48AM
(#3273825)
I forked a version of Kaminari a few months ago to solve this problem for me that I just updated with the latest upstream: https://github.com/joshcutler/kaminari
I added a class method for Mongoid::Documents called limit_max_count_results_to that limits the size of Kaminari's total_count
By the way, if you do .limit() / .offset() pagination, by the time you get into really high offsets, queries will start going slow again. Mongo has to do a scan up to the document at offset that you are asking for.
it does, if you change your pagination interface to only care if there are more results after this page...