Skip to content

Commit

Permalink
DOCS-410 count and index use/performance
Browse files Browse the repository at this point in the history
  • Loading branch information
kay-kim committed Aug 29, 2014
1 parent d7bce5a commit 7310f5c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
39 changes: 39 additions & 0 deletions source/includes/fact-count-index-use.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Consider a collection with the following index:

.. code-block:: javascript
{ a: 1, b: 1 }
When performing a count, MongoDB can return the count using only the
index if:

- the query can use an index,

- the query only contains conditions on the keys of the index, *and*

- the query predicates access a single contiguous range of index keys.

For example, the following operations can return the count using only
the index:

.. code-block:: javascript
db.collection.find( { a: 5, b: 5 } ).count()
db.collection.find( { a: { $gt: 5 } } ).count()
db.collection.find( { a: 5, b: { $gt: 10 } } ).count()
If, however, the query can use an index but the query predicates do not
access a single contiguous range of index keys or the query also
contains conditions on fields outside the index, then in addition to
using the index, MongoDB must also read the documents to return the
count.

.. code-block:: javascript
db.collection.find( { a: 5, b: { $in: [ 1, 2, 3 ] } } ).count()
db.collection.find( { a: { $gt: 5 }, b: 5 } ).count()
db.collection.find( { a: 5, b: 5, c: 5 } ).count()
In such cases, during the initial read of the documents, MongoDB pages
the documents into memory such that subsequent calls of the same count
operation will have better performance.
16 changes: 12 additions & 4 deletions source/reference/method/cursor.count.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,34 @@ Definition

.. code-block:: javascript

db.collection.find().count()
db.collection.find(<query>).count()

The :method:`~cursor.count()` method has the following
parameter:

.. include:: /reference/method/cursor.count-param.rst

MongoDB also provides the shell wrapper
:method:`db.collection.count()` for the
``db.collection.find().count()`` construct.
MongoDB also provides an equivalent :method:`db.collection.count()`
as an alternative to the ``db.collection.find(<query>).count()``
construct.

.. seealso:: :method:`cursor.size()`

Behavior
--------

Sharded Clusters
~~~~~~~~~~~~~~~~

.. |count-op| replace:: :method:`~cursor.count()` method

.. include:: /includes/fact-count-on-sharded-clusters.rst

Index Use
~~~~~~~~~

.. include:: /includes/fact-count-index-use.rst

Examples
--------

Expand Down
11 changes: 11 additions & 0 deletions source/reference/method/db.collection.count.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@ Definition

.. include:: /reference/method/db.collection.count-param.rst

The :method:`db.collection.count()` method is equivalent to the
``db.collection.find(<query>).count()`` construct.

.. seealso:: :method:`cursor.count()`

Behavior
--------

Sharded Clusters
~~~~~~~~~~~~~~~~

.. |count-op| replace:: :method:`db.collection.count()`

.. include:: /includes/fact-count-on-sharded-clusters.rst

Index Use
~~~~~~~~~

.. include:: /includes/fact-count-index-use.rst

Examples
--------

Expand Down

0 comments on commit 7310f5c

Please sign in to comment.