-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdb.collection.count.txt
269 lines (153 loc) · 5.92 KB
/
db.collection.count.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
=====================
db.collection.count()
=====================
.. default-domain:: mongodb
.. meta::
:keywords: deprecated
:description: The count method is deprecated and should be replaced by the countDocuments or estimatedDocumentCount method
.. facet::
:name: programming_language
:values: shell
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.collection.count(query, options)
.. |dbcommand| replace:: :dbcommand:`count` command
.. include:: /includes/fact-mongosh-shell-method-alt
.. note::
MongoDB drivers compatible with the 4.0 features deprecate their
respective cursor and collection ``count()`` APIs in favor of new
APIs for ``countDocuments()`` and ``estimatedDocumentCount()``.
For the specific API names for a given driver, see the driver
documentation.
Returns the count of documents that would match a
:method:`~db.collection.find()` query for the collection or view. The
:method:`db.collection.count()` method does not perform the
:method:`~db.collection.find()` operation but instead counts and
returns the number of results that match a query.
Compatibility
-------------
.. |command| replace:: method
This method is available in deployments hosted in the following environments:
.. include:: /includes/fact-environments-atlas-only.rst
.. include:: /includes/fact-environments-atlas-support-all.rst
.. include:: /includes/fact-environments-onprem-only.rst
Syntax
------
This method takes the following parameters:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``query``
- document
- The query selection criteria.
* - ``options``
- document
- Optional. Extra options for modifying the count.
The ``options`` document contains the following fields:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Field
- Type
- Description
* - ``limit``
- integer
- Optional. The maximum number of documents to count.
* - ``skip``
- integer
- Optional. The number of documents to skip before counting.
* - ``hint``
- string or document
- Optional. An index name hint or specification for the query.
* - ``maxTimeMS``
- integer
- Optional. The maximum amount of time to allow the query to run.
* - ``readConcern``
- string
- Optional. Specifies the :term:`read concern`. The default level is
:readconcern:`"local"`.
.. include:: /includes/usage-read-concern-majority.rst
.. include:: /includes/fact-count-readConcern.rst
* - ``collation``
- document
- Optional.
.. include:: /includes/extracts/collation-option.rst
:method:`~db.collection.count()` is equivalent to the
``db.collection.find(query).count()`` construct.
.. seealso::
- :method:`cursor.count()`
- :method:`db.collection.estimatedDocumentCount()`
- :method:`db.collection.countDocuments()`
Behavior
--------
.. _count-method-behavior-query-predicate:
Inaccurate Counts Without Query Predicate
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When you call :method:`~db.collection.count()` without a query
predicate, you may receive inaccurate document counts. Without a query
predicate, :method:`~db.collection.count()` methods return results based
on the collection's metadata, which may result in an approximate count.
In particular,
- On a sharded cluster, the resulting count will not correctly
filter out :term:`orphaned documents <orphaned document>`.
- After an unclean shutdown or :ref:`file copy based initial sync
<replica-set-initial-sync-file-copy-based>`, the count may be
incorrect.
For counts based on collection metadata, see also
:ref:`collStats pipeline stage with the count <collstat-count>`
option.
Count and Transactions
~~~~~~~~~~~~~~~~~~~~~~
You cannot use :dbcommand:`count` and shell helpers
:method:`~cursor.count()` and :method:`db.collection.count()` in
:ref:`transactions <transactions-ops-count>`.
For details, see :ref:`Transactions and Count Operations
<transactions-ops-count>`.
Sharded Clusters
~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/fact-count-on-sharded-clusters-method-db.collection.count.rst
Index Use
~~~~~~~~~
.. include:: /includes/fact-count-index-use.rst
.. _collection-count-accuracy-shutdown:
Accuracy after Unexpected Shutdown
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. |cmd| replace:: :method:`~db.collection.count()`
.. |opt| replace:: count
.. include:: /includes/fact-unexpected-shutdown-accuracy.rst
.. note::
This loss of accuracy only applies to :method:`~db.collection.count()`
operations that do *not* include a query predicate.
.. |operation| replace:: :method:`db.collection.count()`
Client Disconnection
~~~~~~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/4.2-changes-disconnect.rst
Examples
--------
Count all Documents in a Collection
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To count the number of all documents in the ``orders`` collection, use
the following operation:
.. code-block:: javascript
db.orders.count()
This operation is equivalent to the following:
.. code-block:: javascript
db.orders.find().count()
Count all Documents that Match a Query
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Count the number of the documents in the ``orders``
collection with the field ``ord_dt`` greater than ``new
Date('01/01/2012')``:
.. code-block:: javascript
db.orders.count( { ord_dt: { $gt: new Date('01/01/2012') } } )
The query is equivalent to the following:
.. code-block:: javascript
db.orders.find( { ord_dt: { $gt: new Date('01/01/2012') } } ).count()