Overview
このガイドでは、 MongoDB PHPライブラリを使用して、コレクション全体で指定されたフィールドの個別の値を検索する方法を学習できます。
コレクション内では、異なるドキュメントによって単一のフィールドの異なる値が含まれる場合があります。 例、 restaurants
コレクション内の 1 つのドキュメントのborough
値は'Manhattan'
で、別のドキュメントのborough
値は'Queens'
です。 MongoDB PHPライブラリを使用すると、コレクション内の複数のドキュメントにわたってフィールドに含まれるすべての一意の値を検索できます。
サンプル データ
このガイドの例では、 Atlasサンプルデータセットのsample_restaurants
データベースのrestaurants
コレクションを使用します。 PHPアプリケーションからこのコレクションにアクセスするには、Atlas クラスターに接続するMongoDB\Client
をインスタンス化し、 $collection
変数に次の値を割り当てます。
$collection = $client->sample_restaurants->restaurants;
MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 「Atlas を使い始める」ガイドを参照してください。
MongoDB\Collection::distinct()
方式
指定したフィールドの個別の値を検索するには、 MongoDB\Collection::distinct()
メソッドを呼び出し、個別の値を検索するフィールドの名前を渡します。
コレクション全体で個別の値を取得
次の例では、 restaurants
コレクション内のborough
フィールドの個別の値を取得します。
$results = $collection->distinct('borough', []); foreach ($results as $value) { echo json_encode($value), PHP_EOL; }
"Bronx" "Manhattan" "Missing" "Queens" "Staten Island"
この操作では、個別のborough
フィールド値をそれぞれ保存する配列が返されます。 borough
フィールドでは複数のドキュメントが同じ値になっていますが、各値は結果に 1 回だけ表示されます。
指定されたドキュメント全体で個別の値を取得
distinct()
メソッドにクエリフィルターを提供すると、コレクション内のドキュメントのサブセット全体で個別のフィールド値を検索できます。 クエリフィルターは、操作内のドキュメントを一致させるために使用される検索条件を指定する式です。 クエリフィルターの作成の詳細については、 クエリの指定ガイドを参照してください。
次の例では、 cuisine
フィールドの値が'Italian'
であるすべてのドキュメントのborough
フィールドの個別の値を取得します。
$results = $collection->distinct('borough', ['cuisine' => 'Italian']); foreach ($results as $value) { echo json_encode($value), PHP_EOL; }
"Bronx" "Manhattan" "Queens" "Staten Island"
個別の動作の変更
オプション値を指定する配列を渡すことで、 distinct()
メソッドの動作を変更できます。 次の表では、操作をカスタマイズするために設定できるいくつかのオプションについて説明します。
オプション | 説明 |
---|---|
| The collation to use for the operation. To learn more, see the
Collation section of this page. Type: array|object |
| The maximum amount of time in milliseconds that the operation can run. Type: integer |
| The comment to attach to the operation. Type: any valid BSON type |
| The read preference to use for the operation. To learn more, see
Read Preference in the Server manual. Type: MongoDB\Driver\ReadPreference |
| The index to use for the operation. Type: string|object |
次の例では、 borough
フィールド値が'Bronx'
で、かつcuisine
フィールド値が'Pizza'
であるすべてのドキュメントのname
フィールドの個別の値を取得します。 また、操作にコメントを追加するには、オプション配列のcomment
フィールドを指定します。
$query = ['borough' => 'Bronx', 'cuisine' => 'Pizza']; $options = ['comment' => 'Bronx pizza restaurants']; $results = $collection->distinct('name', $query, $options); foreach ($results as $value) { echo json_encode($value), PHP_EOL; }
"$1.25 Pizza" "18 East Gunhill Pizza" "2 Bros" "Aenos Pizza" "Alitalia Pizza Restaurant" "Amici Pizza And Pasta" "Angie'S Cafe Pizza" ...
照合
操作の 照合 を指定するには、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マニュアルの「 照合 」エントリを参照してください。
API ドキュメント
distinct()
メソッドの詳細については、 APIドキュメントのMongoDB\Collection::distinct()
を参照してください。