Overview
在本指南中,您可以学习;了解如何使用MongoDB PHP库来检索集合中文档数量的准确估计数。 以下方法对集合中的文档进行计数:
MongoDB\Collection::countDocuments()
:返回与查询过滤匹配或存在于集合中的文档的确切数量MongoDB\Collection::estimatedDocumentCount()
:返回集合中的估计文档数
样本数据
本指南中的示例使用 Atlas示例数据集的sample_training
数据库中的companies
集合。 要从PHP应用程序访问权限此集合,请实例化一个连接到Atlas 集群的MongoDB\Client
,并将以下值分配给$collection
变量:
$collection = $client->sample_training->companies;
要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅Atlas入门指南。
检索准确的计数
使用MongoDB\Collection::countDocuments()
方法计算集合中的文档数量。 要计算匹配特定搜索条件的文档数量,请将查询过滤传递给countDocuments()
方法。
要学习;了解有关指定查询的更多信息,请参阅《 指定查询》指南。
对所有文档进行计数
要返回集合中所有文档的计数,请将空查询过滤大量传递给countDocuments()
方法,如以下示例所示:
$result = $collection->countDocuments([]); echo 'Number of documents: ', $result;
Number of documents: 9500
对特定文档进行计数
要返回匹配特定搜索条件的文档计数,请将查询过滤传递给countDocuments()
方法。
以下示例计算founded_year
字段的值为2010
的文档数量:
$result = $collection->countDocuments(['founded_year' => 2010]); echo 'Number of companies founded in 2010: ', $result;
Number of companies founded in 2010: 33
自定义计数行为
您可以通过传递指定选项值的大量来修改countDocuments()
方法的行为。 下表描述了可用于自设立计数操作的一些选项:
选项 | 说明 |
---|---|
| The collation to use for the operation. To learn more,
see the Collation section of this page. Type: array|object |
| The index to use for the operation. Type: string|array|object |
| The comment to attach to the operation. Type: any valid BSON type |
| The maximum number of documents to count. This value must be a positive integer. Type: integer |
| The maximum amount of time in milliseconds that the operation can run. Type: integer |
| The number of documents to skip before counting documents. Type: integer |
| The read preference to use for the operation. To learn more, see
Read Preference in the Server manual. Type: MongoDB\Driver\ReadPreference |
以下示例使用countDocuments()
方法计算number_of_employees
字段值为50
的文档数量,并指示操作最多计算100
结果:
$result = $collection->countDocuments( ['number_of_employees' => 50], ['limit' => 100], ); echo 'Number of companies with 50 employees: ', $result;
Number of companies with 50 employees: 100
排序规则
要为操作指定排序规则,请传递 $options
大量参数,该参数将 collation
选项设置为操作方法。将 collation
选项分配给配置排序规则规则的大量。
下表描述了可以设立以配置排序规则的字段:
字段 | 说明 |
---|---|
| (Required) Specifies the International Components for Unicode (ICU) locale. For a
list of supported locales, see Collation Locales and Default Parameters
in the MongoDB Server manual. Data Type: string |
| (Optional) Specifies whether to include case comparison. When set to true , the comparison behavior depends on the value of
the strength field:- If strength is 1 , the PHP library compares basecharacters and case. - If strength is 2 , the PHP library compares basecharacters, diacritics, other secondary differences, and case. - If strength is any other value, this field is ignored.When set to false , the PHP library doesn't include case comparison at
strength level 1 or 2 .Data Type: bool Default: false |
| (Optional) Specifies the sort order of case differences during tertiary
level comparisons. Data Type: string Default: "off" |
| (Optional) Specifies the level of comparison to perform, as defined in the
ICU documentation. Data Type: int Default: 3 |
| (Optional) Specifies whether the driver compares numeric strings as numbers. If set to true , the PHP library compares numeric strings as numbers.
For example, when comparing the strings "10" and "2", the library uses the
strings' numeric values and treats "10" as greater than "2".If set to false , the PHP library compares numeric strings
as strings. For example, when comparing the strings "10" and "2", the library
compares one character at a time and treats "10" as less than "2".For more information, see Collation Restrictions
in the MongoDB Server manual. Data Type: bool Default: false |
| (Optional) Specifies whether the library considers whitespace and punctuation as base
characters for comparison purposes. Data Type: string Default: "non-ignorable" |
| (Optional) Specifies which characters the library considers ignorable when
the alternate field is set to "shifted" .Data Type: string Default: "punct" |
| (Optional) Specifies whether strings containing diacritics sort from the back of the string
to the front. Data Type: bool Default: false |
检索估计计数
您可以通过调用MongoDB\Collection::estimatedDocumentCount()
方法来检索集合中文档数量的估计值。 该方法根据集合元数据估计文档数量,这可能比执行精确计数更快。
以下示例估计集合中的文档数量:
$result = $collection->estimatedDocumentCount(); echo 'Estimated number of documents: ', $result;
Estimated number of documents: 9500
自定义估计计数行为
您可以通过传递指定选项值的大量作为参数来修改estimatedDocumentCount()
方法的行为。 下表描述了可在大量中设立的选项:
选项 | 说明 |
---|---|
| The comment to attach to the operation. Type: any valid BSON type |
| The maximum amount of time in milliseconds that the operation can run. Type: integer |
| The read concern to use for the operation. To learn more, see
Read Concern in the Server manual. Type: MongoDB\Driver\ReadConcern |
| The read preference to use for the operation. To learn more, see
Read Preference in the Server manual. Type: MongoDB\Driver\ReadPreference |
| The client session to associate with the operation. Type: MongoDB\Driver\Session |
以下示例使用estimatedDocumentCount()
方法返回集合中文档数量的估计值,并将操作超时设置为1000
毫秒:
$result = $collection->estimatedDocumentCount(['maxTimeMS' => 1000]); echo 'Estimated number of documents: ', $result;
Estimated number of documents: 9500
API 文档
要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: