Skip to content

Commit 30dfdcd

Browse files
DOCSP-27257 Add tip to avoid collScan on countDocuments (#611)
1 parent d179217 commit 30dfdcd

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

source/usage-examples/count.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ accepts a :doc:`query </fundamentals/crud/query-document>` parameter.
3333
settings that affect the method's execution. Refer to the reference
3434
documentation for each method for more information.
3535

36-
.. important::
36+
.. tip::
3737

38-
If you require an exact document count in your collection, use a query
39-
to take advantage of the built-in index on the ``_id`` field.
38+
You can improve performance when using ``countDocuments()`` to return the
39+
total number of documents in a collection by avoiding a collection scan. To
40+
do this, use a :manual:`hint </reference/method/cursor.hint>` to take
41+
advantage of the built-in index on the ``_id`` field. Use this technique only
42+
when calling ``countDocuments()`` with an empty query parameter.
4043

41-
.. code-block:: js
44+
.. code-block:: javascript
4245

43-
myColl.countDocuments({ "_id": { "$exists": true } });
46+
collection.countDocuments({}, { hint: "_id_" });
4447

4548
Example
4649
-------

0 commit comments

Comments
 (0)