/
db.collection.count.txt
225 lines (127 loc) · 5.09 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
=====================
db.collection.count()
=====================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.collection.count(query, options)
.. 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.
.. important::
- Avoid using the :method:`db.collection.count()` method without
a query predicate since without the query predicate, the method
returns 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>`.
- :ref:`After an unclean shutdown
<collection-count-accuracy-shutdown>`, the count may be
incorrect.
- For counts based on collection metadata, see also
:ref:`collStats pipeline stage with the count <collstat-count>`
option.
.. 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.
.. versionadded:: 2.6
* - ``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/fact-enable-majority-readConcern.rst
.. include:: /includes/usage-read-concern-majority.rst
.. include:: /includes/fact-count-readConcern.rst
.. versionadded:: 3.2
: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 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.
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()