MongoCollection::count
(PECL mongo >=0.9.0)
MongoCollection::count — Counts the number of documents in this collection
Description
$query
= array()
[, array $options
= array()
]] ) : intParameters
-
query
-
Associative array or object with fields to match.
-
options
-
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 integer The maximum number of matching documents to return. maxTimeMS integer 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 integer The number of matching documents to skip before returning results.
Errors/Exceptions
Throws MongoResultException if the server could not execute the command due to an error.
Throws MongoExecutionTimeoutException if command execution was terminated due to maxTimeMS.
Changelog
Version | Description |
---|---|
1.6.0 |
The second parameter is now an options array.
Passing limit and skip as
the second and third parameters, respectively, is deprecated.
|
1.0.7 |
Added limit and skip as
second and third parameters, respectively.
|
Examples
Example #1 MongoCollection::count() example
<?php
$collection->insert(array('x'=>1));
$collection->insert(array('x'=>2));
$collection->insert(array('x'=>3));
var_dump($collection->count());
var_dump($collection->count(array('x'=>1)));
?>
The above example will output something similar to:
int(3) int(1)
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-mongocollection.count.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.