自版本 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
混合
提示
字符串|数组|对象
要使用的索引。将索引名称指定为字符串或将索引键模式指定为文档。如果指定,查询系统将只考虑使用提示索引的计划。
在版本 1.2 中进行了更改:
如果提供文档,则将其原样传递给命令。以前,该库会将密钥模式转换为索引名。
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.
会话
与操作相关联的客户端会话。
1.3版本新增。
跳过
整型
返回结果之前要跳过的匹配文档数。
Return Values
与筛选条件匹配的文档数量。
错误/异常
MongoDB\Exception\UnexpectedValueException ,如果来自服务器的命令响应格式错误。
MongoDB\Exception\UnsupportedException,如果所选服务器使用了选项但不支持选项(例如collation、readConcern、writeConcern)。
MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。
MongoDB\ 驱动程序\Exception\RuntimeException 用于扩展级别的其他错误(例如连接错误)。
行为
This method is deprecated and cannot be executed within a transaction. It has always been implemented using the count command. The behavior of the count command differs depending on the options passed to it and may or may not provide an accurate count. When no query filter is provided, the count command provides an estimate using collection metadata. Even when provided with a query filter the count command can return inaccurate results with a sharded cluster if orphaned documents exist or if a chunk migration is in progress. The MongoDB\Collection::countDocuments() method avoids these sharded cluster problems entirely.
在评估查询条件时, MongoDB会根据自己的BSON 类型比较规则来比较类型和值,这与 PHP 的比较和类型规则不同。匹配特殊BSON类型时,查询条件应使用扩展中相应的BSON类(例如使用MongoDB\BSON\ObjectId来匹配ObjectId)。
另请参阅
MongoDB 手册中的count命令参考