Overview
在本指南中,您可以学习;了解如何使用.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; } [] public string? Name { get; set; } [] public string? Cuisine { get; set; } [] public string? Borough { get; set; } }
Retrieve Distinct Values
要检索指定字段的非重复值,请调用 IMongoCollection<TDocument>实例的 Distinct() 或 DistinctAsync() 方法,并传递要查找其非重复值的字段的名称。
检索集合中的值
以下示例检索 restaurants集合中 borough字段的非重复值。 选择 Synchronous 或 Asynchronous标签页以查看相应的代码。
var results = collection.Distinct<string>(r => r.Borough, Builders<Restaurant>.Filters.Empty).ToList(); foreach (var result in results) { Console.WriteLine(result); }
var results = await collection.DistinctAsync<string>(r => r.Borough, Builders<Restaurant>.Filters.Empty); await results.ForEachAsync(result => Console.WriteLine(result));
该操作会返回一个游标,您可以遍历该游标以访问权限每个不同的 borough字段值。 尽管多个文档在 borough字段中具有相同的值,但每个值仅在结果中出现一次。
检索指定文档中的值
您可以为 Distinct() 和 DistinctAsync() 方法提供查询过滤,以在集合的文档子集中查找不同的字段值。查询过滤是一个表达式,用于指定在操作中匹配文档的搜索条件。有关创建查询过滤的更多信息,请参阅“创建查询筛选器”指南。
以下示例检索 cuisine字段值为 "Italian" 的所有文档的 borough字段的非重复值。 选择 Synchronous 或 Asynchronous标签页以查看相应的代码。
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); }
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));
修改不同行为
您可以通过提供 DistinctOptions实例作为可选参数来修改 Distinct() 和 DistinctAsync() 方法的行为。 下表描述了您可以在 DistinctOptions实例上设立的属性:
方法 | 说明 |
|---|---|
| |
| 设置操作可以运行的最长时间。 |
| Attaches a comment to the operation. |
以下示例检索 borough字段值为 "Bronx" 且 cuisine字段值为 "Pizza" 的所有文档的 name字段的非重复值。 然后,它通过为 Distinct() 方法提供 DistinctOptions实例来为操作添加注释。
选择 Synchronous 或 Asynchronous 标签页,查看相应的代码。
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); }
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));
排序规则
要为操作配置排序规则,请创建 Collation 类的实例。
下表描述了 Collation 构造函数接受的参数。它还列出了相应的类属性,您可以使用这些属性读取每个设置的值。
Parameter | 说明 | 类属性 |
|---|---|---|
| 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. |
|
| (可选)指定是否包含大小写比较。 |
|
| (Optional) Specifies the sort order of case differences during tertiary level comparisons. |
|
| (Optional) Specifies the level of comparison to perform, as defined in the ICU documentation. |
|
| (Optional) Specifies whether the driver compares numeric strings as numbers. |
|
| (Optional) Specifies whether the driver considers whitespace and punctuation as base characters for purposes of comparison. |
|
| (Optional) Specifies which characters the driver considers ignorable when the |
|
| (Optional) Specifies whether the driver normalizes text as needed. |
|
| (可选)指定包含变音符号的字符串是否从字符串的后部到前部排序。 |
|
有关排序规则的更多信息,请参阅MongoDB Server手册中的排序规则页面。
API 文档
要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: