(PECL mongo >=0.9.0)

MongoCollection::countCounts the number of documents in this collection


public MongoCollection::count ( array $query = array() , array $options = array() ) : int



Associative array or object with fields to match.


An array of options for the index creation. Currently available options include:

Name Type Description
hint mixed

Index to use for the query. If a string is passed, it should correspond to an index name. If an array or object is passed, it should correspond to the specification used to create the index (i.e. the first argument to MongoCollection::createIndex()).

This option is only supported in MongoDB 2.6+.
limit int The maximum number of matching documents to return.
maxTimeMS int

Specifies a cumulative time limit in milliseconds for processing the operation (does not include idle time). If the operation is not completed within the timeout period, a MongoExecutionTimeoutException will be thrown.

This option is only supported in MongoDB 2.6+.
skip int The number of matching documents to skip before returning results.

Return Values

Returns the number of documents matching the query.


Throws MongoResultException if the server could not execute the command due to an error.

Throws MongoExecutionTimeoutException if command execution was terminated due to maxTimeMS.


Version Description
PECL mongo 1.6.0 The second parameter is now an options array. Passing limit and skip as the second and third parameters, respectively, is deprecated.
PECL mongo 1.0.7 Added limit and skip as second and third parameters, respectively.


Example #1 MongoCollection::count() example





The above example will output something similar to:

add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top