collection.count_documents() should accept same kwargs as collection.find()

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Works as Designed
    • Priority: Minor - P4
    • None
    • Affects Version/s: 4.16.0
    • Component/s: Query Operations
    • Python Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Problem

      `pymongo.collection.Collection.count_documents()` does not accept `min` and `max`, even though these kwargs can be passed to `pymongo.collection.Collection.find()` and can alter the document count. This makes `count_documents()` useless for certain queries.

      Definition of half-way done

      `pymongo.collection.Collection.count_documents()` accepts kwargs `min` and `max` and returns the correct number of documents.

      Definition of fully done

      `pymongo.collection.Collection.count_documents()` accepts all kwargs of `pymongo.collection.Collection.find()` and returns the correct document count when users write:
      ```
      find_kwargs = {...}
      with pymongo.MongoClient(host, port) as mongo:
          coll = mongo[database][collection]
          n_docs = coll.count_documents(**find_kwargs)
          with coll.find(**find_kwargs) as cursor:
              # do stuff    
      ```

      Idea for alternative, best solution

      `pymongo.collection.Collection.find()` itself should give access to the total number of matched documents. This improvement would make `pymongo.collection.Collection.count_documents()` redundant.

            Assignee:
            Jib Adegunloye
            Reporter:
            Dimitrios Tsevas
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: