views:

20

answers:

1

Is there a preferred way to query mongo with a limit and know whether there will be more results if I query for the next page with skip/limit?

What I have been doing is asking for one more document than I need, slicing it off the end, and using the existence of that extra document to know whether another query will give at least one more result.

n = 10
docs = db.documents.find({'foo': 'bar'}).limit(n+1)
more = docs.count() > n
docs = docs[:n]

I feel like this is a common use case (knowing whether or not to show an "more results" button on a website) and I feel stupid with my current solution.

+1  A: 

MongoDB has tailable cursors, which allow you to reuse a cursor after all data has been returned. It would look something like this:

n = 10
docs = db.documents.find({"foo": "bar"}).limit(n)
more = docs.hasNext()

Note that there are only 10 documents retrieved, but the cursor can be inspected to determine if there are more objects available. The only problem is that tailable cursors can only be used on capped collections.

The above can also be used with a regular cursor, but you'd have to query for n + 1 documents. This is basically the same solution as you're using now. You have to use size() though, as that takes the skip and limit modifiers into account.

n = 10
docs = db.documents.find({"foo": "bar"}).limit(n + 1)
more = db.size() > n

I'm not familiar with PyMongo, so I don't know this for sure, but there's a possibility that this solution sends n + 1 full documents to your application, rather than the required n, resulting in minor bandwidth overhead. If that's the case, you may want to create a server-side function that does the same, but only returns an object containing n documents in an array, and a flag that indicates if an n + 1th document is available.

Niels van der Rest