Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1762

Document that limit=0 (the default) is equivalent to setting no limit

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 3.8
    • Affects Version/s: None
    • Component/s: API
    • None

      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.

       

            Assignee:
            bernie@mongodb.com Bernie Hackett
            Reporter:
            jason.coombs@yougov.com Jason R. Coombs
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: