1.4版本新增。
定义
参数
$filter: array|object- 指定要计数的文档的筛选条件。
$options: array指定所需选项的数组。
名称类型说明排序规则
数组|对象
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the
localefield is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.如果未指定排序规则但集合具有默认排序规则,则操作将使用为集合指定的排序规则。如果没有为集合或操作指定排序规则,MongoDB 将使用先前版本中用于字符串比较的简单二进制比较。
comment
混合
提示
字符串|数组|对象
要使用的索引。将索引名称指定为字符串或将索引键模式指定为文档。如果指定,查询系统将只考虑使用提示索引的计划。
limit
整型
要返回的匹配文档的最大数量。
maxTimeMS
整型
The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.
事务外的
Read concern to use for the operation. Defaults to the collection's read concern.
无法将单个操作的读关注(read concern)指定为ACID 事务的一部分。相反,请在启动ACID 事务时设立
readConcern选项。readPreference
Read preference to use for the operation. Defaults to the collection's read preference.
会话
与操作相关联的客户端会话。
跳过
整型
返回结果之前要跳过的匹配文档数。
Return Values
与筛选条件匹配的文档数量。
错误/异常
MongoDB\Exception\UnexpectedValueException ,如果来自服务器的命令响应格式错误。
MongoDB\Exception\UnsupportedException,如果所选服务器使用了选项但不支持选项(例如collation、readConcern、writeConcern)。
MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。
MongoDB\ 驱动程序\Exception\RuntimeException 用于扩展级别的其他错误(例如连接错误)。
行为
在内部,该方法使用$group聚合管道操作符来获取结果。 如果给出了filter参数,则会将其转换为$match管道操作符。 可选的$skip和$limit阶段如果存在于选项中,则添加在$match和group之间。
注意
此方法对服务器端的文档进行计数。 要获得不带筛选器的大致文档总数,可以使用MongoDB\Collection::estimatedDocumentCount()方法。 此方法根据collection元数据估计文档数量,因此会为了性能而牺牲准确性。
由于此方法使用聚合管道,因此无法使用MongoDB\Collection::count() filter中接受的某些查询运算符。 请考虑使用以下受限操作符的替代方案:
在评估查询条件时, MongoDB会根据自己的BSON 类型比较规则来比较类型和值,这与 PHP 的比较和类型规则不同。匹配特殊BSON类型时,查询条件应使用扩展中相应的BSON类(例如使用MongoDB\BSON\ObjectId来匹配ObjectId)。