对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

计算文档

在本指南中,您可以学习;了解如何使用MongoDB PHP库来检索集合中文档数量的准确估计数。 以下方法对集合中的文档进行计数:

  • MongoDB\Collection::countDocuments():返回与查询过滤匹配或存在于集合中的文档的确切数量

  • MongoDB\Collection::estimatedDocumentCount():返回集合中的估计文档数

The examples in this guide use the companies collection in the sample_training database from the Atlas sample datasets. To access this collection from your PHP application, instantiate a MongoDB\Client that connects to an Atlas cluster and assign the following value to your $collection variable:

$collection = $client->sample_training->companies;

要了解如何创建免费的 MongoDB 部署并加载示例数据集,请参阅 MongoDB入门指南。

使用MongoDB\Collection::countDocuments()方法计算集合中的文档数量。 要计算匹配特定搜索条件的文档数量,请将查询过滤传递给countDocuments()方法。

要了解有关指定查询的更多信息,请参阅指定查询指南。

要返回集合中所有文档的计数,请将空查询过滤大量传递给countDocuments()方法,如以下示例所示:

$result = $collection->countDocuments([]);
echo 'Number of documents: ', $result;

要返回匹配特定搜索条件的文档计数,请将查询过滤传递给countDocuments()方法。

以下示例计算founded_year字段的值为2010的文档数量:

$result = $collection->countDocuments(['founded_year' => 2010]);
echo 'Number of companies founded in 2010: ', $result;

您可以通过传递指定选项值的大量来修改countDocuments()方法的行为。 下表描述了可用于自设立计数操作的一些选项:

选项
说明

collation

The collation to use for the operation. To learn more, see the Collation section of this page.
Type: array|object

hint

用于操作的索引。
类型:string|array|object

comment

要添加到操作的注释。
类型:任何有效 BSON 类型

limit

要计数的最大文档数。此值必须为正整数。
类型:integer

maxTimeMS

操作可以运行的最长时间(毫秒)。
类型:integer

skip

在对文档进行计数之前要跳过的文档数。
类型:integer

readPreference

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;

要为操作指定排序规则,请传递 $options大量参数,该参数将 collation 选项设置为操作方法。将 collation 选项分配给配置排序规则规则的大量。

下表描述了可以设立以配置排序规则的字段:

字段
说明

locale

(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

caseLevel

(可选)指定是否包含大小写比较。

当设置为true 时,比较行为取决于strength 字段的值:

- 如果strength 为 ,则1 PHP
库比较基本字符和大小写。

- 如果strength 为2 ,则 PHP
库比较基本字符、变音符、其他从节点差异和大小写。

- 如果strength 为任何其他值,则忽略此字段。

当设置为 false 时,PHP 库不包含强度级别 1 或 2 的大小写比较。

数据类型:bool
默认值:false

caseFirst

(可选)指定三级比较期间大小写差异的排序顺序。

数据类型:string
默认:"off"

strength

(可选) 指定要执行的比较级别,如 ICU 文档 中所定义。

数据类型:int
默认:3

numericOrdering

(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

alternate

(可选)指定库是否将空格和标点视为基本字符以进行比较。

数据类型:string
默认:"non-ignorable"

maxVariable

(可选)指定当 alternate 字段设置为 "shifted" 时,库认为可忽略的字符。

数据类型:string
默认值:"punct"

backwards

(可选)指定包含变音符号的字符串是否从字符串的后部到前部排序。

数据类型:bool
默认:false

要学习;了解有关排序规则和每个字段可能值的更多信息,请参阅MongoDB Server手册中的排序规则条目。

您可以通过调用MongoDB\Collection::estimatedDocumentCount()方法来检索集合中文档数量的估计值。 该方法根据集合元数据估计文档数量,这可能比执行精确计数更快。

以下示例估计集合中的文档数量:

$result = $collection->estimatedDocumentCount();
echo 'Estimated number of documents: ', $result;

您可以通过传递指定选项值的大量作为参数来修改estimatedDocumentCount()方法的行为。 下表描述了可在大量中设立的选项:

选项
说明

comment

要添加到操作的注释。
类型:任何有效 BSON 类型

maxTimeMS

操作可以运行的最长时间(毫秒)。
类型:integer

readConcern

The read concern to use for the operation. To learn more, see Read Concern in the Server manual.
Type: MongoDB\Driver\ReadConcern

readPreference

The read preference to use for the operation. To learn more, see Read Preference in the Server manual.
Type: MongoDB\Driver\ReadPreference

session

与操作相关联的客户端会话。
类型:MongoDB\Driver\Session

以下示例使用estimatedDocumentCount()方法返回集合中文档数量的估计值,并将操作超时设置为1000毫秒:

$result = $collection->estimatedDocumentCount(['maxTimeMS' => 1000]);
echo 'Estimated number of documents: ', $result;

要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: