Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 906 Bytes

fact-count-on-sharded-clusters.rst

File metadata and controls

30 lines (23 loc) · 906 Bytes

On a sharded cluster, |count-op| can result in an inaccurate count if :term:`orphaned documents <orphaned document>` exist or if a :doc:`chunk migration </core/sharding-chunk-migration>` is in progress.

To avoid these situations, on a sharded cluster, use the :pipeline:`$group` stage of the :method:`db.collection.aggregate()` method to :group:`$sum` the documents. For example, the following operation counts the documents in a collection:

db.collection.aggregate(
   [
      { $group: { _id: null, count: { $sum: 1 } } }
   ]
)

To get a count of documents that match a query condition, include the :pipeline:`$match` stage as well:

db.collection.aggregate(
   [
      { $match: <query condition> },
      { $group: { _id: null, count: { $sum: 1 } } }
   ]
)

See :ref:`match-perform-a-count` for an example.