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
照合
操作の 照合 を指定するには、collation
オプションを設定する $options
配列パラメータを操作メソッドに渡します。照合ルールを構成する配列に 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 Serverマニュアルの「 照合 」エントリを参照してください。
推定カウントの取得
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 ドキュメントを参照してください。