Overview
在本指南中,您可以学习;了解如何使用MongoDB PHP库检索集合中指定字段的不同值。
在集合中,不同文档的单个字段可能包含不同值。 示例, restaurants
集合中的一个文档的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
字段中具有相同的值,但每个值仅在结果中出现一次。
检索指定文档中的不同值
您可以为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" ...
排序规则
要为操作指定排序规则,请传递 $options
大量参数,该参数将 collation
选项设置为操作方法。将 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()
。