|
| 1 | +=============== |
| 2 | +Count Documents |
| 3 | +=============== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn how to get an :ref:`accurate |
| 17 | +<accurate-count-go>` and :ref:`estimated <estimated-count-go>` count of |
| 18 | +the number of documents in your collection. |
| 19 | + |
| 20 | +Sample Data |
| 21 | +~~~~~~~~~~~ |
| 22 | + |
| 23 | +To run the example in this guide, load the sample data into the |
| 24 | +``ratings`` collection of the ``tea`` database with the following |
| 25 | +snippet: |
| 26 | + |
| 27 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/countAndEstimate.go |
| 28 | + :language: go |
| 29 | + :dedent: |
| 30 | + :start-after: begin insert docs |
| 31 | + :end-before: end insert docs |
| 32 | + |
| 33 | +.. include:: /includes/fundamentals/automatic-db-coll-creation.rst |
| 34 | + |
| 35 | +Each document contains a rating for a type of tea that corresponds to |
| 36 | +the ``type`` and ``rating`` fields. |
| 37 | + |
| 38 | +.. _accurate-count-go: |
| 39 | + |
| 40 | +Accurate Count |
| 41 | +-------------- |
| 42 | + |
| 43 | +To count the number of documents that match your query filter, use the |
| 44 | +``CountDocuments()`` function. |
| 45 | + |
| 46 | +.. tip:: |
| 47 | + |
| 48 | + If you pass an empty query filter, this function returns the total |
| 49 | + number of documents in the collection. |
| 50 | + |
| 51 | +Modify Behavior |
| 52 | +~~~~~~~~~~~~~~~ |
| 53 | + |
| 54 | +You can modify the behavior of ``CountDocuments()`` by passing in a |
| 55 | +``CountOptions`` type. If you don't specify any options, the driver uses |
| 56 | +its default values. |
| 57 | + |
| 58 | +The ``CountOptions`` type allows you to configure options with the |
| 59 | +following functions: |
| 60 | + |
| 61 | +.. list-table:: |
| 62 | + :widths: 30 70 |
| 63 | + :header-rows: 1 |
| 64 | + |
| 65 | + * - Function |
| 66 | + - Description |
| 67 | + |
| 68 | + * - ``SetCollation()`` |
| 69 | + - | The type of language collation to use when sorting results. |
| 70 | + | Default: ``nil`` |
| 71 | + |
| 72 | + * - ``SetHint()`` |
| 73 | + - | The index to use to scan for documents to count. |
| 74 | + | Default: ``nil`` |
| 75 | + |
| 76 | + * - ``SetLimit()`` |
| 77 | + - | The maximum number of documents to count. |
| 78 | + | Default: ``0`` |
| 79 | + |
| 80 | + * - ``SetMaxTime()`` |
| 81 | + - | The maximum amount of time that the query can run on the server. |
| 82 | + | Default: ``nil`` |
| 83 | + |
| 84 | + * - ``SetSkip()`` |
| 85 | + - | The number of documents to skip before counting. |
| 86 | + | Default: ``0`` |
| 87 | + |
| 88 | +Example |
| 89 | +``````` |
| 90 | + |
| 91 | +The following example counts the number of documents where the |
| 92 | +``rating`` is less than ``6``: |
| 93 | + |
| 94 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/countAndEstimate.go |
| 95 | + :language: go |
| 96 | + :dedent: |
| 97 | + :start-after: begin count documents |
| 98 | + :end-before: end count documents |
| 99 | + |
| 100 | +After running this example, the output resembles the following: |
| 101 | + |
| 102 | +.. code-block:: none |
| 103 | + :copyable: false |
| 104 | + |
| 105 | + Number of ratings less than six: 4 |
| 106 | + |
| 107 | +Aggregation |
| 108 | +----------- |
| 109 | + |
| 110 | +You can also include the :manual:`$count </reference/operator/aggregation/count/>` |
| 111 | +stage to count the number of documents in an aggregation pipeline. |
| 112 | + |
| 113 | +Example |
| 114 | +~~~~~~~ |
| 115 | + |
| 116 | +The following example performs the following actions: |
| 117 | + |
| 118 | +- Counts the number of documents where the ``rating`` is greater than ``5`` |
| 119 | +- Assigns the count to a field called ``total_documents`` |
| 120 | + |
| 121 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/countAndEstimate.go |
| 122 | + :language: go |
| 123 | + :dedent: |
| 124 | + :start-after: begin aggregate count |
| 125 | + :end-before: end aggregate count |
| 126 | + |
| 127 | +After running this example, the output resembles the following: |
| 128 | + |
| 129 | +.. code-block:: none |
| 130 | + :copyable: false |
| 131 | + |
| 132 | + [{total_documents 5}] |
| 133 | + |
| 134 | +.. _estimated-count-go: |
| 135 | + |
| 136 | +Estimated Count |
| 137 | +--------------- |
| 138 | + |
| 139 | +To estimate the number of documents in your collection, use the |
| 140 | +``EstimatedDocumentCount()`` function. |
| 141 | + |
| 142 | +.. note:: |
| 143 | + |
| 144 | + The ``EstimatedDocumentCount()`` function is quicker than the |
| 145 | + ``CountDocuments()`` function because it uses the collection's |
| 146 | + metadata rather than scanning the entire collection. |
| 147 | + |
| 148 | +Modify Behavior |
| 149 | +~~~~~~~~~~~~~~~ |
| 150 | + |
| 151 | +You can modify the behavior of ``EstimatedDocumentCount()`` by passing |
| 152 | +in an ``EstimatedDocumentCountOptions`` type. If you don't specify any |
| 153 | +options, the driver uses its default values. |
| 154 | + |
| 155 | +The ``EstimatedDocumentCountOptions`` type allows you to configure |
| 156 | +options with the following functions: |
| 157 | + |
| 158 | +.. list-table:: |
| 159 | + :widths: 30 70 |
| 160 | + :header-rows: 1 |
| 161 | + |
| 162 | + * - Function |
| 163 | + - Description |
| 164 | + |
| 165 | + * - ``SetMaxTime()`` |
| 166 | + - | The maximum amount of time that the query can run on the server. |
| 167 | + | Default: ``nil`` |
| 168 | + |
| 169 | +Example |
| 170 | +``````` |
| 171 | + |
| 172 | +The following example estimates the number of documents in the |
| 173 | +``ratings`` collection: |
| 174 | + |
| 175 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/countAndEstimate.go |
| 176 | + :language: go |
| 177 | + :dedent: |
| 178 | + :start-after: begin est doc count |
| 179 | + :end-before: end est doc count |
| 180 | + |
| 181 | +After running this example, the output resembles the following: |
| 182 | + |
| 183 | +.. code-block:: none |
| 184 | + :copyable: false |
| 185 | + |
| 186 | + Estimated number of documents in the ratings collection: 9 |
| 187 | + |
| 188 | +Additional Information |
| 189 | +---------------------- |
| 190 | + |
| 191 | +For more information on the operations mentioned, see the following |
| 192 | +guides: |
| 193 | + |
| 194 | +- :doc:`Specify a Query </fundamentals/crud/query-document>` |
| 195 | +- :doc:`Skip Returned Results </fundamentals/crud/read-operations/skip>` |
| 196 | +- :doc:`Limit the Number of Returned Results </fundamentals/crud/read-operations/limit>` |
| 197 | + |
| 198 | +.. - :doc:`Aggregation </fundamentals/aggregation>` |
| 199 | +.. - :doc:`Collations </fundamentals/collations>` |
| 200 | + |
| 201 | +API Documentation |
| 202 | +~~~~~~~~~~~~~~~~~ |
| 203 | + |
| 204 | +For more information on any of the functions or types discussed in this |
| 205 | +guide, see the following API Documentation: |
| 206 | + |
| 207 | +- `CountDocuments() <{+api+}/mongo#Collection.CountDocuments>`__ |
| 208 | +- `CountOptions <{+api+}/mongo/options#CountOptions>`__ |
| 209 | +- `EstimatedDocumentCount() <{+api+}/mongo#Collection.EstimatedDocumentCount>`__ |
| 210 | +- `EstimatedDocumentCountOptions <{+api+}/mongo/options#EstimatedDocumentCountOptions>`__ |
0 commit comments