MongoDBCollection-count.txt 2.13 KB
Newer Older
1 2 3 4
============================
MongoDB\\Collection::count()
============================

5 6
.. deprecated:: 1.4

7 8 9 10 11 12 13 14 15 16 17
.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

18
.. phpmethod:: MongoDB\\Collection::count()
19

20
   Count the number of documents that match the filter criteria.
21 22 23 24 25

   .. code-block:: php

      function count($filter = [], array $options = []): integer

26
   This method has the following parameters:
27 28

   .. include:: /includes/apiargs/MongoDBCollection-method-count-param.rst
29 30

   The ``$options`` parameter supports the following options:
31 32 33

   .. include:: /includes/apiargs/MongoDBCollection-method-count-option.rst

34 35
Return Values
-------------
36

37
The number of documents matching the filter criteria.
38

39 40 41 42 43 44 45 46
Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-unexpectedvalueexception.rst
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

47 48 49
Behavior
--------

50 51 52 53 54 55 56 57 58 59 60
This method is deprecated and cannot be executed within a transaction. It has
always been implemented using the :manual:`count </reference/command/count>`
command. The behavior of the ``count`` command differs depending on the options
passed to it and may or may not provide an accurate count. When no query filter
is provided, the ``count`` command provides an estimate using collection
metadata. Even when provided with a query filter the ``count`` command can
return inaccurate results with a sharded cluster if orphaned documents exist or
if a chunk migration is in progress. The
:phpmethod:`MongoDB\\Collection::countDocuments()` method avoids these sharded
cluster problems entirely when used with MongoDB 3.6+, and when a primary read
preference with older sharded clusters.
61

62
.. include:: /includes/extracts/note-bson-comparison.rst
63

64 65
See Also
--------
66

67 68
- :manual:`count </reference/command/count>` command reference in the MongoDB
  manual
69 70
- :phpmethod:`MongoDB\\Collection::countDocuments()`
- :phpmethod:`MongoDB\\Collection::estimatedDocumentCount()`