Skip to content

Commit 20dfadd

Browse files
committed
DOCS-12090: add restrictions for countDocuments
1 parent 444178d commit 20dfadd

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

source/includes/table-transactions-operations.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@
2020
* - :method:`db.collection.countDocuments()`
2121
-
2222

23-
- Uses the :pipeline:`$group` aggregation stage with a
24-
:group:`$sum` expression to perform a count.
23+
- Excluding the following query operator expressions:
24+
25+
- :query:`$where`
26+
- :query:`$near`
27+
- :query:`$nearSphere`
28+
29+
The method uses the :pipeline:`$match` aggregation stage for the
30+
query and :pipeline:`$group` aggregation stage with a
31+
:group:`$sum` expression to perform the count.
2532

2633
* - :method:`db.collection.distinct()`
2734
- :dbcommand:`distinct`

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Definition
3838
- document
3939

4040
- The query selection criteria. To count all documents, specify
41-
an empty document.
41+
an empty document. See also :ref:`countDocuments-restrictions`.
4242

4343
* - options
4444
- document
@@ -74,6 +74,9 @@ Definition
7474
Behavior
7575
--------
7676

77+
Mechanics
78+
~~~~~~~~~
79+
7780
Unlike :method:`db.collection.count()`,
7881
:method:`db.collection.countDocuments()` does not use the metadata to
7982
return the count. Instead, it performs an aggregation of the document
@@ -94,6 +97,52 @@ aggregation operation and returns just the value of ``n``:
9497
:method:`db.collection.countDocuments()` errors on an empty or
9598
non-existing collection or view.
9699

100+
.. _countDocuments-restrictions:
101+
102+
Query Restrictions
103+
~~~~~~~~~~~~~~~~~~
104+
105+
You cannot use the following query operators as part of the query
106+
expression for :method:`db.collection.countDocuments()`:
107+
108+
.. list-table::
109+
:header-rows: 1
110+
:widths: 30 70
111+
112+
* - Restricted Operator
113+
- Alternative
114+
115+
* - :query:`$where`
116+
117+
- As an alternative, use :query:`$expr` instead.
118+
119+
* - :query:`$near`
120+
121+
- As an alternative, use :query:`$geoWithin` with :query:`$center`; i.e.
122+
123+
.. code-block:: javascript
124+
:copyable: false
125+
126+
{ $geoWithin: { $center: [ [ <x>, <y> ], <radius> ] } }
127+
128+
* - :query:`$nearSphere`
129+
130+
- As an alternative, use :query:`$geoWithin` with :query:`$centerSphere`; i.e.
131+
132+
.. code-block:: javascript
133+
:copyable: false
134+
135+
{ $geoWithin: { $centerSphere: [ [ <x>, <y> ], <radius> ] } }
136+
137+
Transactions
138+
~~~~~~~~~~~~
139+
140+
.. include:: /includes/extracts/transactions-supported-operation.rst
141+
142+
.. include:: /includes/extracts/transactions-usage.rst
143+
144+
.. |operation| replace:: :method:`db.collection.countDocuments()`
145+
97146
Examples
98147
--------
99148

source/reference/operator/aggregation/match.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,21 @@ Restrictions
4949
~~~~~~~~~~~~
5050

5151
- You cannot use :query:`$where` in :pipeline:`$match` queries as part
52-
of the aggregation pipeline.
52+
of the aggregation pipeline. As an alternative, use :query:`$eval`
53+
operator instead.
54+
55+
- You cannot use :query:`$near` or :query:`$nearSphere` in
56+
:pipeline:`$match` queries as part of the aggregation pipeline. As an
57+
alternative, you can either:
58+
59+
- Use :pipeline:`$geoNear` stage instead of the :pipeline:`$match` stage.
60+
61+
- Use :query:`$geoWithin` query operator with :query:`$center` or
62+
:query:`$centerSphere` in the :pipeline:`$match` stage.
5363

5464
- To use :query:`$text` in the :pipeline:`$match` stage, the
5565
:pipeline:`$match` stage has to be the first stage of the pipeline.
56-
66+
5767
.. include:: /includes/extracts/views-unsupported-text-search.rst
5868

5969
Examples

0 commit comments

Comments
 (0)