文档菜单
文档首页
/ / /
PHP 库手册
/ /

MongoDB\Collection::distinct()

本页

  • 定义
  • 参数
  • 返回值
  • 错误/异常
  • 行为
  • 示例
  • 另请参阅
MongoDB\Collection::distinct()

在集合中查找指定字段的唯一值。

function distinct(
string $fieldName,
array|object $filter = [],
array $options = []
): mixed[]
$fieldName : string
返回唯一值的字段。
$filter : array|object
指定从哪些文档中检索唯一值的筛选条件。
$options : array

指定所需选项的数组。

名称
类型
描述
collation
array|object

校对允许用户指定针对字符串比较的语言特定规则,例如字母大小写和重音符号的规则。在指定校对时,locale字段是必需的;所有其他校对字段都是可选的。有关字段的说明,请参阅校对文档

如果未指定校对,但集合具有默认校对,则操作使用为集合指定的校对。如果集合或操作未指定校对,MongoDB将使用先前版本中用于字符串比较的简单二进制比较。

注释
混合

启用用户指定任意注释以帮助通过数据库分析器当前操作输出和日志追踪操作。

此选项自MongoDB 4.4起可用,如果指定用于较旧的服务器版本,则将在执行时引发异常。

版本1.13.

maxTimeMS
整数

对游标上的操作进行处理的累积时间限制(以毫秒为单位)。MongoDB将在最早的下一次中断点处终止操作。

readConcern

读关注用于操作。默认为集合的读关注。

无法在事务中将读关注指定为单个操作的组成部分。相反,在启动事务时设置readConcern选项。

readPreference

读优先级用于操作。默认为集合的读优先级。

session

与操作关联的客户端会话。

版本1.3.

typeMap
数组

类型映射 应用于游标,这决定了 BSON 文档如何转换为 PHP 值。默认为集合的类型映射。

版本1.5.

不同值的数组。

MongoDB\Exception\UnexpectedValueException 如果命令响应来自服务器的数据格式不正确。

MongoDB\Exception\UnsupportedException 如果使用了所选服务器不支持选项(例如 collationreadConcernwriteConcern)。

MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。

MongoDB\Driver\Exception\RuntimeException 用于扩展级别其他错误(例如连接错误)。

When evaluating query criteria, MongoDB compares types and values according to its own comparison rules for BSON types, which differs from PHP's comparison and type juggling rules. When matching a special BSON type the query criteria should use the respective BSON class in the extension (e.g. use MongoDB\BSON\ObjectId to match an ObjectId).

以下示例标识了在测试数据库中restaurants集合中borough字段的唯一值。

<?php
$collection = (new MongoDB\Client)->test->restaurants;
$distinct = $collection->distinct('borough');
var_dump($distinct);

输出将类似于

array(6) {
[0]=>
string(5) "Bronx"
[1]=>
string(8) "Brooklyn"
[2]=>
string(9) "Manhattan"
[3]=>
string(7) "Missing"
[4]=>
string(6) "Queens"
[5]=>
string(13) "Staten Island"
}

以下示例标识了在测试数据库中restaurants集合中cuisine字段的唯一值,条件是文档中的boroughQueens

<?php
$collection = (new MongoDB\Client)->test->restaurants;
$distinct = $collection->distinct('cuisine', ['borough' => 'Queens']);
var_dump($distinct);

输出将类似于

array(75) {
[0]=>
string(6) "Afghan"
[1]=>
string(7) "African"
[2]=>
string(9) "American "
[3]=>
string(8) "Armenian"
[4]=>
string(5) "Asian"
[5]=>
string(10) "Australian"
[6]=>
string(15) "Bagels/Pretzels"
[7]=>
string(6) "Bakery"
[8]=>
string(11) "Bangladeshi"
[9]=>
string(8) "Barbecue"
[10]=>
string(55) "Bottled beverages, including water, sodas, juices, etc."
[11]=>
string(9) "Brazilian"
[12]=>
string(4) "Cafe"
[13]=>
string(16) "Café/Coffee/Tea"
[14]=>
string(5) "Cajun"
[15]=>
string(9) "Caribbean"
[16]=>
string(7) "Chicken"
[17]=>
string(7) "Chinese"
[18]=>
string(13) "Chinese/Cuban"
[19]=>
string(16) "Chinese/Japanese"
[20]=>
string(11) "Continental"
[21]=>
string(6) "Creole"
[22]=>
string(5) "Czech"
[23]=>
string(12) "Delicatessen"
[24]=>
string(6) "Donuts"
[25]=>
string(16) "Eastern European"
[26]=>
string(8) "Egyptian"
[27]=>
string(7) "English"
[28]=>
string(8) "Filipino"
[29]=>
string(6) "French"
[30]=>
string(17) "Fruits/Vegetables"
[31]=>
string(6) "German"
[32]=>
string(5) "Greek"
[33]=>
string(10) "Hamburgers"
[34]=>
string(16) "Hotdogs/Pretzels"
[35]=>
string(31) "Ice Cream, Gelato, Yogurt, Ices"
[36]=>
string(6) "Indian"
[37]=>
string(10) "Indonesian"
[38]=>
string(5) "Irish"
[39]=>
string(7) "Italian"
[40]=>
string(8) "Japanese"
[41]=>
string(13) "Jewish/Kosher"
[42]=>
string(30) "Juice, Smoothies, Fruit Salads"
[43]=>
string(6) "Korean"
[44]=>
string(64) "Latin (Cuban, Dominican, Puerto Rican, South & Central American)"
[45]=>
string(13) "Mediterranean"
[46]=>
string(7) "Mexican"
[47]=>
string(14) "Middle Eastern"
[48]=>
string(8) "Moroccan"
[49]=>
string(25) "Not Listed/Not Applicable"
[50]=>
string(18) "Nuts/Confectionary"
[51]=>
string(5) "Other"
[52]=>
string(9) "Pakistani"
[53]=>
string(16) "Pancakes/Waffles"
[54]=>
string(8) "Peruvian"
[55]=>
string(5) "Pizza"
[56]=>
string(13) "Pizza/Italian"
[57]=>
string(6) "Polish"
[58]=>
string(10) "Portuguese"
[59]=>
string(7) "Russian"
[60]=>
string(6) "Salads"
[61]=>
string(10) "Sandwiches"
[62]=>
string(30) "Sandwiches/Salads/Mixed Buffet"
[63]=>
string(7) "Seafood"
[64]=>
string(9) "Soul Food"
[65]=>
string(18) "Soups & Sandwiches"
[66]=>
string(12) "Southwestern"
[67]=>
string(7) "Spanish"
[68]=>
string(5) "Steak"
[69]=>
string(5) "Tapas"
[70]=>
string(7) "Tex-Mex"
[71]=>
string(4) "Thai"
[72]=>
string(7) "Turkish"
[73]=>
string(10) "Vegetarian"
[74]=>
string(29) "Vietnamese/Cambodian/Malaysia"
}
  • distinct 命令参考,请参阅MongoDB手册

返回

deleteOne()