============================
MongoDB\\Collection::count()
============================

.. deprecated:: 1.4

.. default-domain:: mongodb

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

Definition
----------

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

   Count the number of documents that match the filter criteria.

   .. code-block:: php

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

Parameters
----------

``$filter`` : array|object
  The filter criteria that specifies the documents to count.

``$options`` : array
  An array specifying the desired options.

  .. list-table::
     :header-rows: 1
     :widths: 20 20 80

     * - Name
       - Type
       - Description

     * - collation
       - array|object
       - .. include:: /includes/extracts/collection-option-collation.rst

     * - comment
       - mixed
       - .. include:: /includes/extracts/common-option-comment.rst

         .. include:: /includes/extracts/option-requires-4.4.rst

         .. versionadded:: 1.13

     * - hint
       - string|array|object
       - .. include:: /includes/extracts/common-option-hint.rst

         .. versionchanged:: 1.2

            If a document is provided, it is passed to the command as-is.
            Previously, the library would convert the key pattern to an index
            name.

     * - limit
       - integer
       - The maximum number of matching documents to return.

     * - maxTimeMS
       - integer
       - .. include:: /includes/extracts/common-option-maxTimeMS.rst

     * - readConcern
       - :php:`MongoDB\Driver\ReadConcern <class.mongodb-driver-readconcern>`
       - .. include:: /includes/extracts/collection-option-readConcern.rst

         .. include:: /includes/extracts/common-option-readConcern-transaction.rst

     * - readPreference
       - :php:`MongoDB\Driver\ReadPreference <class.mongodb-driver-readpreference>`
       - .. include:: /includes/extracts/collection-option-readPreference.rst

     * - session
       - :php:`MongoDB\Driver\Session <class.mongodb-driver-session>`
       - .. include:: /includes/extracts/common-option-session.rst

         .. versionadded:: 1.3

     * - skip
       - integer
       - The number of matching documents to skip before returning results.

Return Values
-------------

The number of documents matching the filter criteria.

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

Behavior
--------

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.

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

See Also
--------

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