Docs Menu
Docs Home
/ / /
PHP Library Manual
/ /

MongoDB\Collection::estimatedDocumentCount()

On this page

  • Definition
  • Parameters
  • Return Values
  • Errors/Exceptions
  • Behavior
  • See Also

New in version 1.4.

MongoDB\Collection::estimatedDocumentCount()

Gets an estimated number of documents in the collection using collection metadata.

function countDocuments(array $options = []): integer
$options : array

An array specifying the desired options.

Name
Type
Description
comment
mixed

Enables users to specify an arbitrary comment to help trace the operation through the database profiler, currentOp output, and logs.

This option is available since MongoDB 4.4 and will result in an exception at execution time if specified for an older server version.

New in version 1.13.

maxTimeMS
integer

The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.

readConcern

Read concern to use for the operation. Defaults to the collection's read concern.

It is not possible to specify a read concern for individual operations as part of a transaction. Instead, set the readConcern option when starting the transaction.

readPreference

Read preference to use for the operation. Defaults to the collection's read preference.

session

Client session to associate with the operation.

An estimated number of documents in the collection.

MongoDB\Exception\UnexpectedValueException if the command response from the server was malformed.

MongoDB\Exception\UnsupportedException if options are used and not supported by the selected server (e.g. collation, readConcern, writeConcern).

MongoDB\Exception\InvalidArgumentException for errors related to the parsing of parameters or options.

MongoDB\Driver\Exception\RuntimeException for other errors at the extension level (e.g. connection errors).

This method returns an estimate of the count of documents in the collection using collection metadata, rather than counting the documents or consulting an index. This method does not take a session option and cannot be executed within a transaction. See Count: Behavior in the MongoDB manual for more information.

This method is implemented using the count command. Due to an oversight in versions 5.0.0-5.0.8 of MongoDB, the count command was not included in version "1" of the Stable API. Applications using this method with the Stable API are recommended to upgrade their server version to 5.0.9+ or disable strict mode to avoid encountering errors.

Back

dropSearchIndex()