Skip to content

Commit 444178d

Browse files
committed
DOCS-11947: countDocuments and estimatedDocumentCount
1 parent 81d03d2 commit 444178d

12 files changed

+264
-12
lines changed

config/redirects

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,8 +1425,8 @@ raw: /master/release-notes/3.0-general-improvements -> ${base}/release-notes/3.0
14251425
[v3.0-v3.6]: /${version}/tutorial/upgrade-to-enterprise-replica-set -> ${base}/${version}/installation
14261426
[v3.0-v3.6]: /${version}/tutorial/upgrade-to-enterprise-sharded-cluster -> ${base}/${version}/installation
14271427

1428-
1429-
1428+
[*-v3.6]: /${version}/reference/method/db.collection.countDocuments -> ${base}/${version}/reference/operator/aggregation/count
1429+
[*-v3.6]: /${version}/reference/method/db.collection.estimatedDocumentCount -> ${base}/${version}/reference/method/db.collection.count
14301430
[*-v3.6]: /${version}/reference/method/db.watch -> ${base}/${version}/changeStreams
14311431
[*-v3.6]: /${version}/reference/method/Mongo.watch -> ${base}/${version}/changeStreams
14321432

source/aggregation.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ can also output to a sharded collection. See
9595
Single Purpose Aggregation Operations
9696
-------------------------------------
9797

98-
MongoDB also provides :method:`db.collection.count()` and
99-
:method:`db.collection.distinct()`.
98+
MongoDB also provides :method:`db.collection.estimatedDocumentCount()`,
99+
:method:`db.collection.count()` and :method:`db.collection.distinct()`.
100100

101101
All of these operations aggregate documents from a single collection.
102102
While these operations provide simple access to common aggregation

source/core/transactions.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ collection-level API ``countDocuments(filter, options)`` as a helper
105105
method that uses the :pipeline:`$group` with a :group:`$sum` expression
106106
to perform a count. The 4.0 drivers have deprecated the ``count()`` API.
107107

108+
Starting in MongoDB 4.0.3, the :binary:`~bin.mongo` shell provides the
109+
:method:`db.collection.countDocuments()` helper method that uses the
110+
:pipeline:`$group` with a :group:`$sum` expression to perform a count.
111+
108112
.. _transactions-ops-info:
109113

110114
Informational Operations

source/core/views.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,7 @@ restrictions mentioned in this page:
101101
- | :method:`db.collection.aggregate()`
102102
| :method:`db.collection.find()`
103103
| :method:`db.collection.findOne()`
104+
| :method:`db.collection.countDocuments()`
105+
| :method:`db.collection.estimatedDocumentCount()`
104106
| :method:`db.collection.count()`
105107
| :method:`db.collection.distinct()`

source/includes/extracts-views.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ content: |
1313
- :method:`db.collection.find()`
1414
- :method:`db.collection.findOne()`
1515
- :method:`db.collection.aggregate()`
16+
- :method:`db.collection.countDocuments()`
17+
- :method:`db.collection.estimatedDocumentCount()`
1618
- :method:`db.collection.count()`
1719
- :method:`db.collection.distinct()`
1820

source/includes/ref-toc-method-collection.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ name: ":method:`db.collection.count()`"
1414
file: /reference/method/db.collection.count
1515
description: "Wraps :dbcommand:`count` to return a count of the number of documents in a collection or a view."
1616
---
17+
name: ":method:`db.collection.countDocuments()`"
18+
file: /reference/method/db.collection.countDocuments
19+
description: |
20+
Wraps the :pipeline:`$group` aggregation stage with a :group:`$sum`
21+
expression to return a count of the number of documents in a
22+
collection or a view.
23+
---
24+
name: ":method:`db.collection.estimatedDocumentCount()`"
25+
file: /reference/method/db.collection.estimatedDocumentCount
26+
description: "Wraps :dbcommand:`count` to return an approximate count of the documents in a collection or a view."
27+
---
1728
name: ":method:`db.collection.createIndex()`"
1829
file: /reference/method/db.collection.createIndex
1930
description: "Builds an index on a collection."

source/includes/table-transactions-operations.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
- :pipeline:`$listSessions`
1818
- :pipeline:`$out`
1919

20+
* - :method:`db.collection.countDocuments()`
21+
-
22+
23+
- Uses the :pipeline:`$group` aggregation stage with a
24+
:group:`$sum` expression to perform a count.
25+
2026
* - :method:`db.collection.distinct()`
2127
- :dbcommand:`distinct`
2228
-

source/reference/command/count.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,20 @@ Definition
4444

4545
.. include:: /includes/apiargs/dbcommand-count-field.rst
4646

47-
MongoDB also provides the :method:`~cursor.count()` and
48-
:method:`db.collection.count()` wrapper methods in the
49-
:binary:`~bin.mongo` shell.
47+
The :binary:`~bin.mongo` shell also provides the following wrapper methods for :dbcommand:`count`:
48+
49+
- :method:`~cursor.count()`
50+
- :method:`db.collection.estimatedDocumentCount()`
51+
- :method:`db.collection.count()`
5052

5153
.. important::
5254

5355
- Avoid using the :dbcommand:`count` and its wrapper methods
54-
without a query predicate since without the query predicate,
55-
these operations return results based on the collection's
56-
metadata, which may result in an approximate count. In
57-
particular,
56+
without a query predicate (note:
57+
:method:`db.collection.estimatedDocumentCount()` does not take
58+
a query predicate) since without the query predicate, these
59+
operations return results based on the collection's metadata,
60+
which may result in an approximate count. In particular,
5861

5962
- On a sharded cluster, the resulting count will not correctly
6063
filter out :term:`orphaned documents <orphaned document>`.

source/reference/method/db.collection.count.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ Definition
5656
:method:`~db.collection.count()` is equivalent to the
5757
``db.collection.find(query).count()`` construct.
5858

59-
.. seealso:: :method:`cursor.count()`
59+
.. seealso::
60+
61+
- :method:`cursor.count()`
62+
- :method:`db.collection.estimatedDocumentCount()`
63+
- :method:`db.collection.countDocuments()`
6064

6165
Behavior
6266
--------
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
==============================
2+
db.collection.countDocuments()
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+
Definition
14+
----------
15+
16+
.. method:: db.collection.countDocuments(query, options)
17+
18+
.. versionadded:: 4.0.3
19+
20+
Returns the count of documents that match the query for a collection
21+
or view. The method wraps the :pipeline:`$group` aggregation stage
22+
with a :group:`$sum` expression to perform the count and is
23+
available for use in :doc:`/core/transactions`.
24+
25+
.. code-block:: javascript
26+
27+
db.collection.countDocuments( <query>, <options> )
28+
29+
.. list-table::
30+
:header-rows: 1
31+
:widths: 20 20 80
32+
33+
* - Parameter
34+
- Type
35+
- Description
36+
37+
* - query
38+
- document
39+
40+
- The query selection criteria. To count all documents, specify
41+
an empty document.
42+
43+
* - options
44+
- document
45+
- Optional. Extra options that affects the count behavior.
46+
47+
The ``options`` document can contain the following:
48+
49+
.. list-table::
50+
:header-rows: 1
51+
:widths: 20 20 80
52+
53+
* - Field
54+
- Type
55+
- Description
56+
57+
* - ``limit``
58+
- integer
59+
- Optional. The maximum number of documents to count.
60+
61+
* - ``skip``
62+
- integer
63+
- Optional. The number of documents to skip before counting.
64+
65+
* - ``hint``
66+
- string or document
67+
68+
- Optional. An index name or the index specification to use for the query.
69+
70+
* - ``maxTimeMS``
71+
- integer
72+
- Optional. The maximum amount of time to allow the count to run.
73+
74+
Behavior
75+
--------
76+
77+
Unlike :method:`db.collection.count()`,
78+
:method:`db.collection.countDocuments()` does not use the metadata to
79+
return the count. Instead, it performs an aggregation of the document
80+
to return an accurate count, even after an unclean shutdown or in the
81+
presence of :term:`orphaned documents <orphaned document>` in a sharded
82+
cluster.
83+
84+
:method:`db.collection.countDocuments()` wraps the following
85+
aggregation operation and returns just the value of ``n``:
86+
87+
.. code-block:: javascript
88+
89+
db.collection.aggregate([
90+
{ $match: <query> },
91+
{ $group: { _id: null, n: { $sum: 1 } } } )
92+
])
93+
94+
:method:`db.collection.countDocuments()` errors on an empty or
95+
non-existing collection or view.
96+
97+
Examples
98+
--------
99+
100+
Count all Documents in a Collection
101+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102+
103+
To count the number of all documents in the ``orders`` collection, use
104+
the following operation:
105+
106+
.. code-block:: javascript
107+
108+
db.orders.countDocuments({})
109+
110+
Count all Documents that Match a Query
111+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112+
113+
Count the number of the documents in the ``orders``
114+
collection with the field ``ord_dt`` greater than ``new
115+
Date('01/01/2012')``:
116+
117+
.. code-block:: javascript
118+
119+
db.orders.countDocuments( { ord_dt: { $gt: new Date('01/01/2012') } }, { limit: 100 } )
120+
121+
.. seealso::
122+
123+
- :method:`db.collection.estimatedDocumentCount()`
124+
- :pipeline:`$group` and :group:`$sum`
125+
- :dbcommand:`count`
126+
- :ref:`collStats pipeline stage with the count <collstat-count>`
127+
option.
128+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
======================================
2+
db.collection.estimatedDocumentCount()
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+
Definition
14+
----------
15+
16+
.. method:: db.collection.estimatedDocumentCount(options)
17+
18+
.. versionadded:: 4.0.3
19+
20+
Returns the count of all documents in a collection or view. The
21+
method wraps the :dbcommand:`count` command.
22+
23+
.. code-block:: javascript
24+
25+
db.collection.estimatedDocumentCount( <options> )
26+
27+
.. list-table::
28+
:header-rows: 1
29+
:widths: 20 20 80
30+
31+
* - Parameter
32+
- Type
33+
- Description
34+
35+
* - options
36+
- document
37+
- Optional. Extra options that affect the count behavior.
38+
39+
The ``options`` document can contain the following:
40+
41+
.. list-table::
42+
:header-rows: 1
43+
:widths: 20 20 80
44+
45+
* - Field
46+
- Type
47+
- Description
48+
49+
* - ``maxTimeMS``
50+
- integer
51+
- Optional. The maximum amount of time to allow the count to run.
52+
53+
54+
Behavior
55+
--------
56+
57+
:method:`db.collection.estimatedDocumentCount()` does not take a query
58+
filter and instead uses metadata to return the count for a collection.
59+
60+
- On a sharded cluster, the resulting count will not correctly filter
61+
out :term:`orphaned documents <orphaned document>`.
62+
63+
- After an unclean shutdown, the count may be incorrect.
64+
65+
.. include:: /includes/fact-unexpected-shutdown-accuracy.rst
66+
67+
.. |cmd| replace:: :method:`db.collection.estimatedDocumentCount()`
68+
.. |opt| replace:: count
69+
70+
71+
Example
72+
-------
73+
74+
The following example uses
75+
:method:`db.collection.estimatedDocumentCount` to retrieve the count of
76+
all documents in the ``orders`` collection:
77+
78+
.. code-block:: javascript
79+
80+
db.orders.estimatedDocumentCount({})
81+
82+
.. seealso::
83+
84+
- :method:`db.collection.countDocuments()`
85+
- :dbcommand:`count`
86+
- :ref:`collStats pipeline stage with the count <collstat-count>`
87+
option.

source/reference/operator/aggregation/count.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ Definition
3232

3333
.. seealso::
3434

35+
- :method:`db.collection.countDocuments()`
3536
- :pipeline:`$collStats`
37+
- :method:`db.collection.estimatedDocumentCount()`
3638
- :dbcommand:`count`
3739
- :method:`db.collection.count()`
3840

@@ -52,6 +54,9 @@ The :pipeline:`$count` stage is equivalent to the following
5254
where ``myCount`` would be the output field that contains the count.
5355
You can specify another name for the output field.
5456

57+
.. seealso:: :method:`db.collection.countDocuments()` which wraps the
58+
:pipeline:`$group` aggregation stage with a :group:`$sum` expression.
59+
5560
Example
5661
-------
5762

0 commit comments

Comments
 (0)