|
| 1 | +.. _pymongo-count: |
| 2 | + |
| 3 | +=============== |
| 4 | +Count Documents |
| 5 | +=============== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: number, amount, estimation, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +--------- |
| 22 | + |
| 23 | +In this guide, you can learn how to retrieve an accurate and estimated count of the |
| 24 | +number of documents in a collection. |
| 25 | + |
| 26 | +Retrieve an Accurate Count |
| 27 | +-------------------------- |
| 28 | + |
| 29 | +Use the ``count_documents()`` method to count the number of documents that are in a |
| 30 | +collection. To count the number of documents that match a specific search |
| 31 | +critera, pass a dictionary that includes a query filter to the |
| 32 | +``count_documents()`` method. |
| 33 | + |
| 34 | +To learn more about specifying a query, see :ref:`pymongo-specify-query`. |
| 35 | + |
| 36 | +Count All Documents |
| 37 | +~~~~~~~~~~~~~~~~~~~ |
| 38 | + |
| 39 | +To return a count of all documents in the collection, pass an empty dictionary to |
| 40 | +the ``count_documents()`` method, as shown in the following example: |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | + |
| 44 | + collection.count_documents({}) |
| 45 | + |
| 46 | +Count Specific Documents |
| 47 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 48 | + |
| 49 | +To return a count of documents that match specific search criteria, specify your |
| 50 | +query in the ``count_documents()`` method, as shown in the following example: |
| 51 | + |
| 52 | +.. code-block:: python |
| 53 | + |
| 54 | + collection.count_documents({ "author": "Mike" }) |
| 55 | + |
| 56 | +Customize Count Behavior |
| 57 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 58 | + |
| 59 | +The ``count_documents()`` method accepts optional parameters, which represent |
| 60 | +options you can use to configure the count operation. If you don't specify any |
| 61 | +options, the driver does not customize the count operation. |
| 62 | + |
| 63 | +The following table describes the options you can set to customize ``count_documents()``: |
| 64 | + |
| 65 | +.. list-table:: |
| 66 | + :widths: 30 70 |
| 67 | + :header-rows: 1 |
| 68 | + |
| 69 | + * - Property |
| 70 | + - Description |
| 71 | + |
| 72 | + * - ``comment`` |
| 73 | + - | A comment to attach to the operation. |
| 74 | + |
| 75 | + * - ``session`` |
| 76 | + - | An instance of ``ClientSession``. |
| 77 | + |
| 78 | + * - ``skip`` |
| 79 | + - | The number of documents to skip before returning results. |
| 80 | + |
| 81 | + * - ``limit`` |
| 82 | + - | The maximum number of documents to count. Must be a positive integer. |
| 83 | + |
| 84 | + * - ``maxTimeMS`` |
| 85 | + - | The maximum amount of time to allow the operation to run, in |
| 86 | + milliseconds. |
| 87 | + |
| 88 | + * - ``collation`` |
| 89 | + - | An instance of ``Collation``. |
| 90 | + |
| 91 | + * - ``hint`` |
| 92 | + - | Gets or sets the index to scan for documents. |
| 93 | + |
| 94 | +Retrieve an Estimated Count |
| 95 | +--------------------------- |
| 96 | + |
| 97 | +You can get an estimate of the number of documents in a collection by calling |
| 98 | +the ``estimated_document_count()`` method. The method estimates the amount of |
| 99 | +documents based on collection metadata, which might be faster than performing an |
| 100 | +accurate count. |
| 101 | + |
| 102 | +The following example estimates the number of documents in a collection: |
| 103 | + |
| 104 | +.. code-block:: python |
| 105 | + |
| 106 | + collection.estimated_document_count() |
| 107 | + |
| 108 | +Customize Estimated Count Behavior |
| 109 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 110 | + |
| 111 | +The ``estimated_document_count()`` method accepts optional parameters, which |
| 112 | +represent options you can use to configure the count operation. If you don't |
| 113 | +specify any options, the driver does not customize the count operation. |
| 114 | + |
| 115 | +The following table describes the options you can set to customize ``estimated_document_count()``: |
| 116 | + |
| 117 | +.. list-table:: |
| 118 | + :widths: 30 70 |
| 119 | + :header-rows: 1 |
| 120 | + |
| 121 | + * - Property |
| 122 | + - Description |
| 123 | + |
| 124 | + * - ``comment`` |
| 125 | + - | A comment to attach to the operation. |
| 126 | + |
| 127 | + * - ``maxTimeMS`` |
| 128 | + - | The maximum amount of time to allow the operation to run, in |
| 129 | + milliseconds. |
| 130 | + |
| 131 | +API Documentation |
| 132 | +----------------- |
| 133 | + |
| 134 | +To learn more about any of the methods or types discussed in this |
| 135 | +guide, see the following API documentation: |
| 136 | + |
| 137 | +- `count_documents() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.count_documents>`__ |
| 138 | +- `estimated_document_count() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.count_documents>`__ |
| 139 | +- `Collation <{+api-root+}pymongo/collation.html#pymongo.collation.Collation>`__ |
| 140 | +- `ClientSession <{+api-root+}pymongo/client_session.html#pymongo.client_session.ClientSession>`__ |
0 commit comments