Docs 菜单
Docs 主页
/ / /
PHP 库手册
/ /

MongoDB\Collection::distinct()

在此页面上

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

查找collection中指定字段的非重复值。

function distinct(
string $fieldName,
array|object $filter = [],
array $options = []
): mixed[]
$fieldName : 细绳
要针对该字段返回非重复值。
$filter : array|object
筛选条件,指定要从中检索非重复值的文档。
$options : array

指定所需选项的数组。

名称
类型
说明

排序规则

数组|对象

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

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

comment

混合

使用户可以指定任意注释来帮助通过数据库分析器currentOp 输出和日志来跟踪操作。

提示

string|对象

要使用的索引。将索引名称指定为字符串或将索引键模式指定为文档。如果指定,查询系统将只考虑使用提示索引的计划。

maxTimeMS

整型

处理游标操作的累计时间限制(以毫秒为单位)。MongoDB 最早会在中断点之后中止操作。

事务外的

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

readPreference

读取要用于操作的偏好。 默认为集合的读取偏好。

会话

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

typeMap

阵列

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

由不同值组成的数组。

MongoDB\Exception\UnexpectedValueException ,如果来自服务器的命令响应格式错误。

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

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

MongoDB\ 驱动程序\Exception\RuntimeException 对于扩展级别的其他错误(例如连接错误)。

在评估查询条件时,MongoDB 会根据自己 的 比较规则来BSON types 比较类型和值,这不同于 的PHP 比较 类型杂技 规则。匹配特殊BSON类型时,查询条件应使用相应的 BSON类 在扩展(例如使用 MongoDB\ BSON\ObjectId 以匹配 ObjectId )。

以下示例标识了test数据库中restaurantscollection中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"
}

以下示例标识了cuisine restaurantstest数据库中 为 的文档的collection集合中borough Queens字段的非重复值:

<?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"
}
  • MongoDB 手册中的distinct命令参考

后退

DeleteOne()