Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
PHP ライブラリ マニュアル
/ /

個別のフィールド値の取得

このガイドでは、 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()メソッドを呼び出し、個別の値を検索するフィールドの名前を渡します。

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

オプション
説明

collation

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

maxTimeMS

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

comment

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

readPreference

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

hint

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 オプションを割り当てます。

次の表では、照合を構成するために設定できるフィールドについて説明しています。

フィールド
説明

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

(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 base
characters and case.

- If strength is 2, the PHP library compares base
characters, 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

caseFirst

(Optional) Specifies the sort order of case differences during tertiary level comparisons.

Data Type: string
Default: "off"

strength


Data Type: int
Default: 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

(Optional) Specifies whether the library considers whitespace and punctuation as base characters for comparison purposes.

Data Type: string
Default: "non-ignorable"

maxVariable

(Optional) Specifies which characters the library considers ignorable when the alternate field is set to "shifted".

Data Type: string
Default: "punct"

backwards

(Optional) Specifies whether strings containing diacritics sort from the back of the string to the front.

Data Type: bool
Default: false

照合と各フィールドに可能な値の詳細については、 MongoDB Serverマニュアルの「 照合 」エントリを参照してください。

distinct()メソッドの詳細については、 APIドキュメントのMongoDB\Collection::distinct()を参照してください。

戻る

ドキュメントをカウント

項目一覧