File tree Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -33,14 +33,17 @@ accepts a :doc:`query </fundamentals/crud/query-document>` parameter.
33
33
settings that affect the method's execution. Refer to the reference
34
34
documentation for each method for more information.
35
35
36
- .. important ::
36
+ .. tip ::
37
37
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.
40
43
41
- .. code-block:: js
44
+ .. code-block:: javascript
42
45
43
- myColl .countDocuments({ "_id": { "$exists": true } });
46
+ collection .countDocuments({}, { hint: "_id_" });
44
47
45
48
Example
46
49
-------
You can’t perform that action at this time.
0 commit comments