-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
db.collection.count.txt
183 lines (97 loc) · 3.66 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
=====================
db.collection.count()
=====================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.collection.count(query, options)
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.
.. 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()`
Behavior
--------
Sharded Clusters
~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/fact-count-on-sharded-clusters-method-db.collection.count.rst
Index Use
~~~~~~~~~
.. include:: /includes/fact-count-index-use.rst
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()