Docs Menu

ドキュメントをカウント

このガイドでは、 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()メソッドの動作を変更できます。 次の表では、カウント操作をカスタマイズするために設定できるいくつかのオプションについて説明しています。

オプション
説明

collation

The collation to use for the operation.
Type: array|object

hint

The index to use for the operation.
Type: string|array|object

comment

The comment to attach to the operation.
Type: any valid BSON type

limit

The maximum number of documents to count. This value must be a positive integer.
Type: integer

maxTimeMS

The maximum amount of time in milliseconds that the operation can run.
Type: integer

skip

The number of documents to skip before counting documents.
Type: 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;
Number of companies with 50 employees: 100

MongoDB\Collection::estimatedDocumentCount()メソッドを呼び出すと、コレクション内のドキュメント数の推定値を取得できます。 メソッドは、コレクションメタデータに基づいてドキュメントの量を推定します。これは、正確なカウントを実行するよりも高速である可能性があります。

次の例では、 コレクション内のドキュメントの数を見積ります。

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

オプション値を指定する配列をパラメーターとして渡すことで、 estimatedDocumentCount()メソッドの動作を変更できます。 次の表では、 配列に設定できるオプションについて説明しています。

オプション
説明

comment

The comment to attach to the operation.
Type: any valid BSON type

maxTimeMS

The maximum amount of time in milliseconds that the operation can run.
Type: 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

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 ドキュメントを参照してください。