MongoDB\collection::count()
已弃用自版本1.4.
定义
参数
$filter
: array|object- 指定要计数的文档的过滤器条件。
$options
: array指定所需选项的数组。
返回值
符合过滤器条件的文档数量。
错误/异常
MongoDB\Exception\UnexpectedValueException
如果服务器命令响应格式不正确。
MongoDB\Exception\UnsupportedException
如果使用了服务器不支持(例如collation
、readConcern
、writeConcern
)的选项。
MongoDB\Exception\InvalidArgumentException
与参数或选项解析相关的错误。
MongoDB\Driver\Exception\RuntimeException用于扩展级别其他错误(例如连接错误)。
行为
此方法已弃用,无法在事务中执行。它始终使用count命令来实现。count命令的行为取决于传递给它的选项,可能或可能不提供准确的计数。当未提供查询过滤器时,count命令使用集合元数据提供估计。即使提供了查询过滤器,count命令在存在孤立文档或正在进行数据分片迁移的碎片化集群中也可能返回不准确的结果。MongoDB\Collection::countDocuments()
方法可以完全避免这些问题。
When evaluating query criteria, MongoDB compares types and values according to its own comparison rules for BSON types, which differs from PHP's comparison and type juggling rules. When matching a special BSON type the query criteria should use the respective BSON class in the extension (e.g. use MongoDB\BSON\ObjectId to match an ObjectId).