对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

检索不同字段值

在本指南中,您可以学习;了解如何使用.NET/ C#驱动程序检索集合中指定字段的不同值。

在集合中,不同文档的单个字段可能包含不同值。 示例,restaurants集合中一个文档的borough 值为 "Manhattan",而另一文档的 borough 值为 "Queens"。 通过使用.NET/ C#驱动程序,您可以检索集合中多个文档中某个字段包含的所有唯一值。

本指南中的示例使用Atlas示例数据集中的 sample_restaurants.restaurants集合。要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅.NET/ C#驱动程序入门

本页上的示例使用以下 Restaurant 类对集合中的文档进行建模:

public class Restaurant {
public ObjectId? Id { get; set; }
[BsonElement("name")]
public string? Name { get; set; }
[BsonElement("cuisine")]
public string? Cuisine { get; set; }
[BsonElement("borough")]
public string? Borough { get; set; }
}

要检索指定字段的非重复值,请调用 IMongoCollection<TDocument>实例的 Distinct()DistinctAsync() 方法,并传递要查找其非重复值的字段的名称。

以下示例检索 restaurants集合中 borough字段的非重复值。 选择 SynchronousAsynchronous标签页以查看相应的代码。

var results = collection.Distinct<string>(r => r.Borough, Builders<Restaurant>.Filters.Empty).ToList();
foreach (var result in results)
{
Console.WriteLine(result);
}
Bronx
Brooklyn
Manhattan
Missing
Queens
Staten Island
var results = await collection.DistinctAsync<string>(r => r.Borough, Builders<Restaurant>.Filters.Empty);
await results.ForEachAsync(result => Console.WriteLine(result));
Bronx
Brooklyn
Manhattan
Missing
Queens
Staten Island

该操作会返回一个游标,您可以遍历该游标以访问权限每个不同的 borough字段值。 尽管多个文档在 borough字段中具有相同的值,但每个值仅在结果中出现一次。

您可以为 Distinct()DistinctAsync() 方法提供查询过滤,以在集合的文档子集中查找不同的字段值。查询过滤是一个表达式,用于指定在操作中匹配文档的搜索条件。有关创建查询过滤的更多信息,请参阅“创建查询筛选器”指南。

以下示例检索 cuisine字段值为 "Italian" 的所有文档的 borough字段的非重复值。 选择 SynchronousAsynchronous标签页以查看相应的代码。

var filter = Builders<Restaurant>.Filter.Eq(r => r.Cuisine, "Italian");
var results = collection.Distinct<string>(r => r.Borough, filter).ToList();
foreach (var result in results)
{
Console.WriteLine(result);
}
Bronx
Brooklyn
Manhattan
Queens
Staten Island
var filter = Builders<Restaurant>.Filter.Eq(r => r.Cuisine, "Italian");
var results = await collection.DistinctAsync<string>(r => r.Borough, filter);
await results.ForEachAsync(result => Console.WriteLine(result));
Bronx
Brooklyn
Manhattan
Queens
Staten Island

您可以通过提供 DistinctOptions实例作为可选参数来修改 Distinct()DistinctAsync() 方法的行为。 下表描述了您可以在 DistinctOptions实例上设立的属性:

方法
说明

Collation

设置用于操作的排序规则。有关更多信息,请参阅本页的“排序规则”部分。
默认:null
数据类型排序规则

MaxTime

设置操作可以运行的最长时间。
数据类型TimeSpan

Comment

将注释附加到操作。
数据类型BsonValuestring

以下示例检索 borough字段值为 "Bronx"cuisine字段值为 "Pizza" 的所有文档的 name字段的非重复值。 然后,它通过为 Distinct() 方法提供 DistinctOptions实例来为操作添加注释。

选择 SynchronousAsynchronous 标签页,查看相应的代码。

var cuisineFilter = Builders<Restaurant>.Filter.Eq(r => r.Cuisine, "Pizza");
var boroughFilter = Builders<Restaurant>.Filter.Eq(r => r.Borough, "Bronx");
var filter = Builders<Restaurant>.Filter.And(cuisineFilter, boroughFilter);
var options = new DistinctOptions {
Comment = "Find all Italian restaurants in the Bronx"
};
var results = collection.Distinct<string>(r => r.Name, filter).ToList();
foreach (var result in results)
{
Console.WriteLine(result);
}
$1.25 Pizza
18 East Gunhill Pizza
2 Bros
Aenos Pizza
Alitalia Pizza Restaurant
Amici Pizza And Pasta
Angie'S Cafe Pizza
...
var cuisineFilter = Builders<Restaurant>.Filter.Eq(r => r.Cuisine, "Pizza");
var boroughFilter = Builders<Restaurant>.Filter.Eq(r => r.Borough, "Bronx");
var filter = Builders<Restaurant>.Filter.And(cuisineFilter, boroughFilter);
var options = new DistinctOptions {
Comment = "Find all Italian restaurants in the Bronx"
};
var results = await collection.DistinctAsync<string>(r => r.Name, filter, options);
await results.ForEachAsync(result => Console.WriteLine(result));
$1.25 Pizza
18 East Gunhill Pizza
2 Bros
Aenos Pizza
Alitalia Pizza Restaurant
Amici Pizza And Pasta
Angie'S Cafe Pizza
...

要为操作配置排序规则,请创建 Collation 类的实例。

下表描述了 Collation 构造函数接受的参数。它还列出了相应的类属性,您可以使用这些属性读取每个设置的值。

Parameter
说明
类属性

locale

指定国际化组件字符集 (ICU) 区域设置。有关支持的区域设置列表,请参阅 MongoDB Server 手册中的 排序规则区域设置和默认参数

如果要使用简单的二进制比较,请使用 Collation.Simple 静态属性返回 Collation 对象,并将 locale 设置为 "simple"
数据类型: string

Locale

caseLevel

(可选)指定是否包含大小写比较。

当此参数为 true 时,驱动程序的行为取决于 strength 参数的值:

- 如果 strengthCollationStrength.Primary,则驱动程序会比较基本字符和大小写。
- 如果 strengthCollationStrength.Secondary,则驱动程序会比较基本字符、变音符、其他次要差异和大小写。
- 如果 strength 为任何其他值,则会忽略此参数。

当此参数为 false 时,驱动程序不会在强度级别 PrimarySecondary 包含大小写比较。

数据类型boolean
默认值false

CaseLevel

caseFirst

(可选)指定三级比较期间大小写差异的排序顺序。

数据类型CollationCaseFirst
默认值CollationCaseFirst.Off

CaseFirst

strength

(可选) 指定要执行的比较级别,如 ICU 文档 中所定义。

数据类型CollationStrength
默认CollationStrength.Tertiary

Strength

numericOrdering

(可选)指定驱动程序是将数字字符串作为数字还是字符串进行比较。

如果此参数为 true,则驱动程序会将数字字符串作为数字进行比较。例如,在比较字符串 "10" 和 "2" 时,驱动程序会将值视为 10 和 2,并发现 10 大于另一个值。

如果此参数为 false 或被排除,则驱动程序会将数字字符串作为字符串进行比较。例如,在比较字符串 "10" 和 "2" 时,驱动程序会一次比较一个字符。由于 "1" 小于 "2",因此驱动程序会发现 "10" 小于 "2"。

有关详细信息,请参阅 MongoDB Server 手册中的 排序规则限制

数据类型boolean
默认false

NumericOrdering

alternate

(可选) 指定驱动程序是否将空格和标点视为比较的基本字符。

数据类型CollationAlternate
默认值CollationAlternate.NonIgnorable (空格和标点被视为基本字符)

Alternate

maxVariable

(可选)指定当 alternate参数为CollationAlternate.Shifted

时驾驶员认为哪些字符可忽略。数据类型:CollationMaxVariable
默认值:CollationMaxVariable.Punctuation (驾驶员忽略标点符号和空格)

MaxVariable

normalization



(可选)指定驾驶员是否根据需要对文本进行规范化。大多数文本不需要规范化。有关规范化的更多信息,请参阅 ICU 文档。数据类型:

boolean
默认值:false

Normalization

backwards

(可选)指定包含变音符号的字符串是否从字符串的后部到前部排序。

数据类型boolean
默认false

Backwards

有关排序规则的更多信息,请参阅MongoDB Server手册中的排序规则页面。

要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: