Re: Mongodb design for cursor limits to zero and infinity

984 views
Skip to first unread message

Asya Kamsky

unread,
May 13, 2013, 3:40:07 PM5/13/13
to mongod...@googlegroups.com
Limit of 0 means no limit, as documented here: http://docs.mongodb.org/manual/reference/method/cursor.limit/

This provides a simple way to provide a number when no limit is desired - I suppose for consistency of syntax.

I'm not sure about infinity - my guess is behavior with it is undefined (you may get back size()=0 but if you remove size I'm guessing you'll get back one document.

Asya


On Sunday, May 12, 2013 12:11:12 PM UTC-7, alain demour wrote:
It seems that the behavior for limit on cursors both in the mongodb shell and any drivers , but also if you apply a stream is as follow:

> db.things.find({}).count();
7394
> db.things.find({}).limit(3).size();
3
> db.things.find({}).limit(2).size();
2
> db.things.find({}).limit(1).size();
1
> db.things.find({}).limit(0).size();
7394
> db.things.find({}).limit(-1).size();
1
> db.things.find({}).limit(-2).size();
2
> db.things.find({}).limit(Infinity).size();
0

So basically if you limit to zero you get ALL the items in a collection and if you limit to Infinity you get zero. Is there a rationale behind this design choice?


alain demour

unread,
May 13, 2013, 8:54:50 PM5/13/13
to mongod...@googlegroups.com
I can see that there is the spec that says limit of zero means no limit, but it does not explain why it is so ... it seems

1) totally counter intuitive / illogical, if limit(2) returns 2 result, limit(1) one result, limit(0) should return 0  ... so you have to spend hours digging through documentations to find out about some weird counter intuitive rule.

2) breaks iterative algorithms that decreases the  limit of a queries down to zero, or start them at zero (which was my case).

3) Since javascript DOES provide a built-in called Infinity that means exactly that (no limit) should be what defines no limit, that would be intuitive

Asya Kamsky

unread,
May 14, 2013, 3:18:44 AM5/14/13
to mongod...@googlegroups.com
What would be the use case for limit(0)? Why run a no-op query?

alain demour

unread,
May 23, 2013, 8:17:42 PM5/23/13
to mongod...@googlegroups.com
you would never run it explicitly  but as part of a generic algorithm where the limit is computed and arrives as as "in" parameter in some function it should be behave logically which is to return zero results as requested.

The invention of zero in arithmetic (1000 years ago in India if I recall my history classes) was a major scientific advance precisely because of that because it allowed work compute with null quantities no differently than with non null, so 1 + 0 = 0, you could ask why would you ever add zero to one, it does not do anything, right?

Max Schireson

unread,
May 25, 2013, 11:28:12 AM5/25/13
to mongod...@googlegroups.com

Your intuition reflects another sensible way to handle it. Without having been a part of the initial choice myself, one issue in favor of the current approach is trying to keep behavior consistent across languages - for someone using mongodb from a language without an infinity primitive, they should also have an easy way to specify all results within the context of a limit, and there should be minimal change from language to language. Limit 0 meaning all records in some languages and none in others would cause confusion.

In this case a zero limit is treated in a sense like a null limit. I recognize that some will find that counterintuitive, but there wasn't an obvious alternative for many other languages.

Infinity primitive in js not being implemented to have an intuitive behavior I bet is just work not done in favor of other work. Not trivial without an infinity primitive in c++ see http://stackoverflow.com/questions/8690567/setting-an-int-to-infinity-in-c I am sure there are reasonable solutions but not sure at this point in the product cycle it is worth bothering vs implementing something else given another working solution.

Good question, thx.

-- Max

--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

alain demour

unread,
May 26, 2013, 6:15:42 PM5/26/13
to mongod...@googlegroups.com

I don't find the answer very satisfying... so first let's forget about limit(infinity) I was just mentioning it for completeness as I was playing with boundary conditions to see how mongodb behaves there after I started to have problems with limit(0). Let's just not worry about it and let's leave it undefined as it is probably now.  As you said some language don't support it, and this is something that a programmer will NEVER EVER hit accidentally because generating infinity as a limit by running a normal algorithm 1) cannot be done in most languages as you pointed it out 2) even in javascript probably never  happens in the real world,  very few algorithms , if any, iterates in purpose to the upper bound of  8 byte floats .

The issue is limit(0), which is a 100% valid case, that can happens all the time if write any kind of slightly generic code, and here the design choice  flies in the face of  any normal practice established in math over thousand of years of math and science. It totally counter intuitive in ANY language under ANY circumstances, for anybody who ever wrote a 
for i = 0 ; i < n loop.
assembly language also have NOOP operation, for the exact same reason to be able to run "something" that will do nothing ... that's a very common pattern.

This is not happening in strange exotic algorithm, It hit me writing something that generate all unique pairs pulled form a very large mongodb collection (Combination(n,2) for n very large), I had to compute all possible unique pairs of products for a recommendation engine (if you like this you would like that), which should be right in the mongodb sweet spot.

Asya Kamsky

unread,
May 27, 2013, 12:32:52 AM5/27/13
to mongod...@googlegroups.com
Actually, if you think about limit as 0 == false and > 0 == true (which is the old fashioned C way of thinking of booleans) you can see how 0 can mean "no limit".

Limit 0 doesn't seem to be useful to mean return back to me 0 documents.  That is not a very useful operation.  Limit 0 to mean "no limit" makes some sense - it simplifies the logging of limit value passed to query (if you used 0 to mean limit return to no documents how would you represent limit-none?  There isn't a convenient integer value to be used in this case.

Asya
Reply all
Reply to author
Forward
0 new messages