Retrieve Distinct Values检索剔除重复值
On this page本页内容
Overview概述
Use the 使用distinct()
method to retrieve all distinct values for a specified field across a collection.distinct()
方法检索集合中指定字段的所有剔除重复值。
Sample Documents示例文档
To follow along with the examples in this guide, use the following code snippet to insert documents that describe restaurants into the 要遵循本指南中的示例,请使用以下代码片段将描述餐馆的文档插入myDB.restaurants
collection:myDB.restaurants
集合:
const myDB = client.db("myDB");
const myColl = myDB.collection("restaurants");
await myColl.insertMany([
{ "_id": 1, "restaurant": "White Bear", "borough": "Queens", "cuisine": "Chinese" },
{ "_id": 2, "restaurant": "Via Carota", "borough": "Manhattan", "cuisine": "Italian" },
{ "_id": 3, "restaurant": "Borgatti's", "borough": "Bronx", "cuisine": "Italian" },
{ "_id": 4, "restaurant": "Tanoreen", "borough": "Brooklyn", "cuisine": "Middle Eastern" },
{ "_id": 5, "restaurant": "Äpfel", "borough": "Queens", "cuisine": "German" },
{ "_id": 6, "restaurant": "Samba Kitchen", "borough": "Manhattan", "cuisine": "Brazilian" },
]);
Your query operation may return a reference to a cursor that contains matching documents. 您的查询操作可能会返回对包含匹配文档的游标的引用。To learn how to examine data stored in the cursor, see the Cursor Fundamentals page.要了解如何检查存储在游标中的数据,请参阅游标基础页面。
Distinct剔除重复
The distinct()
method requires a document field as a parameter. distinct()
方法需要一个文档字段作为参数。You can specify the following optional parameters to adjust the method output:您可以指定以下可选参数来调整方法输出:
A用于细化结果的query
parameter to refine your resultsquery
参数An用于设置排序规则的options
parameter to set collation rulesoptions
参数
Document Field Parameter文档字段参数
Pass the name of the document field to return a list of the field's unique values.传递文档字段的名称以返回字段的唯一值列表。
Example实例
The "“Queens”和“Manhattan”区的值在样本文档中都出现了不止一次。Queens
" and "Manhattan
" borough values each appear more than once in the sample documents. However, the following example retrieves the unique values of the 但是,以下示例检索borough
field:borough
字段的唯一值:
//specify "borough" as the field to return values for指定“borough”作为要返回值的字段
const cursor = myColl.distinct("borough");
for await (const doc of cursor) {
console.dir(doc);
}
This code outputs the following 此代码输出以下borough
values:borough
值:
[ "Bronx", "Brooklyn", "Manhattan", "Queens" ]
Query Parameter查询参数
You can specify a query parameter to return unique values for documents that match your query.您可以指定一个查询参数,为与查询匹配的文档返回唯一值。
Visit Specify a Query for more information on constructing a query filter.有关构造查询筛选器的详细信息,请访问指定查询。
Example实例
The following example outputs the distinct values of the 以下示例输出了cuisine
field but excludes restaurants in "Brooklyn
":cuisine
字段的的剔除重复值,但不包括“Brooklyn”的餐厅:
//exclude Brooklyn restaurants from the output将布鲁克林餐馆排除在输出之外
const query = { borough: { $ne: "Brooklyn" }};
//find the filtered distinct values of "cuisine"找到“美食”的筛选后的剔除重复值
const cursor = myColl.distinct("cuisine", query);
for await (const doc of cursor) {
console.dir(doc);
}
In this case, the query filter matches every borough value except for "在这种情况下,查询筛选器匹配除“Brooklyn”以外的所有Brooklyn
". borough
值。This prevents 这阻止了distinct()
from outputting one cuisine
value, "Middle Eastern
". distinct()
输出一个cuisine
值“Middle Eastern”。The code outputs the following values:该代码输出以下值:
[ "Brazilian", "Chinese", "German", "Italian" ]
Options Parameter选项参数
You can specify the collation to the 通过将distinct()
method by defining a collation
field as an options
parameter. collation
字段定义为options
参数,可以为distinct()
方法指定排序规则。This field allows you to set regional rules for string ordering and comparisons.此字段允许您设置字符串排序和比较的区域规则。
See Collations for instructions on applying collations.有关应用排序规则的说明,请参阅排序规则。
When using the 使用options
parameter, you must also specify a query
parameter. options
参数时,还必须指定query
参数。If you don't want to use a query filter, define the query as 如果不想使用查询筛选器,请将查询定义为{}
.{}
。
Example实例
The following example uses a 以下示例使用collation
field to specify German language ordering conventions when outputting the distinct restaurant
values:collation
字段来指定输出剔除重复restaurant
值时的德语排序约定:
//define an empty query document定义一个空的查询文档
const query = {};
//specify German string ordering conventions指定德语字符串排序约定
const options = { collation: { locale: "de" }};
const cursor = myColl.distinct("restaurant", query, options);
for await (const doc of cursor) {
console.dir(doc);
}
In this case, German string ordering conventions place words beginning with "Ä" before those beginning with "B". 在这种情况下,德语字符串排序约定将以“Ä”开头的单词放在以“B”开头的词之前。The code outputs the following:该代码输出以下内容:
[ "Äpfel", "Borgatti's", "Samba Kitchen", "Tanoreen", "Via Carota", "White Bear" ]
Without specifying a 如果不指定collation
field, the output order would follow default binary collation rules. collation
字段,输出顺序将遵循默认的二进制排序规则。These rules place words beginning with "Ä" after the those with unaccented first letters:这些规则将以“Ä”开头的单词放在第一个字母未重音的单词后面:
[ "Borgatti's", "Samba Kitchen", "Tanoreen", "Via Carota", "White Bear", "Äpfel" ]
Additional Information附加信息
For a runnable example of retrieving distinct values, see Retrieve Distinct Values of a Field.有关检索剔除重复值的可运行示例,请参阅检索字段的剔除重复值。
API DocumentationAPI文件
To learn more about the 要了解有关distinct()
method and its parameters, you can visit the API documentation.distinct()
方法及其参数的更多信息,可以访问API文档。