Navigation

MongoDB\Collection::estimatedDocumentCount()

New in version 1.4.

Definition

MongoDB\Collection::estimatedDocumentCount

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

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

This method has the following parameters:

Parameter Type Description
$options array Optional. An array specifying the desired options.

The $options parameter supports the following options:

Option Type Description
maxTimeMS integer Optional. The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.
readConcern MongoDB\Driver\ReadConcern

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

This is not supported for server versions prior to 3.2 and will result in an exception at execution time if used.

readPreference MongoDB\Driver\ReadPreference Optional. Read preference to use for the operation. Defaults to the collection’s read preference.
session MongoDB\Driver\Session

Optional. Client session to associate with the operation.

Sessions are not supported for server versions prior to 3.6.

Return Values

An estimated number of documents in the collection.

Errors/Exceptions

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 driver level (e.g. connection errors).

Behavior

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 Also