File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,19 @@ than scanning the entire collection. The ``countDocuments()`` method returns
23
23
an **accurate** count of the number of documents and supports specifying
24
24
a filter.
25
25
26
+ .. tip::
27
+
28
+ When using ``countDocuments()`` to return the total number of documents in a
29
+ collection, you can improve performance by avoiding a collection scan. To do
30
+ this, use a :manual:`hint </reference/method/cursor.hint>` to take advantage
31
+ of the built-in index on the ``_id`` field. Use this technique only when
32
+ calling ``countDocuments()`` with an empty query parameter.
33
+
34
+ .. code-block:: java
35
+
36
+ CountOptions opts = new CountOptions().hintString("_id_");
37
+ long numDocuments = collection.countDocuments(new BsonDocument(), opts);
38
+
26
39
When you call the ``countDocuments()`` method, you can optionally pass a
27
40
**query filter** parameter. You cannot pass any parameters when you call
28
41
``estimatedDocumentCount()``.
You can’t perform that action at this time.
0 commit comments