Somewhere along the way we removed the part of the docstring for the limit option to find() that mentioned that 0 means "all results" not "no results". Let's just use the language from the find command docs.
Original report follows
-----------------------------
The pymongo driver (and possibly other drivers) define the 'limit' parameter on the 'find' method with a default of
limit=0
with the definition, "limit (int): The maximum number of documents to count."
According to the docs, a limit of 0 should return a maximum of zero documents, but in fact returns all available documents. Zero is treated like infinity.
This mechanism is incorrect and dangerous. Consider a situation where the limit is a calculated value to gather a sample:
limit=floor(coll.estimatedDocumentCount / batch_size)
If batch_size is larger than estimatedDocumentCount, that limit will resolve to zero and could return a large cursor instead of an empty one.
This incongruity introduces potential for errors similar in nature to the diskerase queue described here.
Probably better, pymongo should (a) allow zero and honor it as a meaningful value, (b) solicit None and float('inf') as values meaning no limit.
In lieu of correcting the dangerous behavior, the docs should at least document that zero has a special meaning.
Probably some deprecation period will be needed to support an explicit 0 with backward-compatibility.
- related to
-
PYTHON-1763 Change the find() method limit default to None
- Closed