Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB "limit" operator negative value

Tags:

mongodb

What does a negative value for limit operator mean?

http://www.mongodb.org/display/DOCS/Aggregation+Framework+-+$limit

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%7B%7Blimit%28%29%7D%7D

Mongo returns the same exact document when I do limit(1) or limit(-1) ??

like image 242
kapso Avatar asked Aug 16 '12 18:08

kapso


1 Answers

If the limit number is negative, then generally the database will return that number of results and close the cursor - essentially a single batch of results is returned and no further results for that query can be fetched.

As for the less general case, if the negative limit value exceeds the batch size (particularly the max batch size), then the batch will be returned and the cursor closed whether the limit has been reached or not. Hence, the single batch rule trumps the limit specified if the limit is too high.

If the limit is positive you can leave the cursor open to receive further results and continue to iterate until the cursor is exhausted.

For more on batches and cursors, take a look here:

http://docs.mongodb.org/manual/core/cursors/#cursor-batches http://docs.mongodb.org/manual/reference/method/cursor.limit/#negative-values

like image 197
Adam Comerford Avatar answered Oct 16 '22 11:10

Adam Comerford