AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

個別のフィールド値の取得

このガイドでは、 .NET/ C#ドライバーを使用して、コレクション全体で指定されたフィールドの個別の値を検索する方法を学習できます。

コレクション内では、異なるドキュメントによって単一のフィールドの異なる値が含まれる場合があります。 例、restaurantsコレクション内の 1 つのドキュメントの 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フィールドの個別の値を取得します。 対応するコードを表示するには、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フィールドでは複数のドキュメントが同じ値になっていますが、各値は結果に 1 回だけ表示されます。

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インスタンスに設定できるプロパティについて説明します。

方式
説明

Collation

操作に使用する 照合 を設定します。詳細については、このページの「 照合 」セクションを参照してください。デフォルト:
null
データ型: 照合

MaxTime

操作を実行する最大時間を設定します。
データ型TimeSpan

Comment


操作にコメントを付けます。データ型: BsonValue またはstring

次の例では、 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);
}
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 コンストラクターが受け入れるパラメーターを説明しています。また、各設定の値を読み取るために使用できる対応するクラスプロパティも一覧表示されます。

Parameter
説明
クラスプロパティ

locale

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.

If you want to use simple binary comparison, use the Collation.Simple static property to return a Collation object with the locale set to "simple".
Data Type: string

Locale

caseLevel

(任意) 大文字と小文字の比較を含めるかどうかを指定します。

この引数が true の場合、ドライバーの動作は strength 引数の値によって異なります。

- strengthCollationStrength.Primary の場合、ドライバーは基本文字と大文字と小文字を比較します。
- strengthCollationStrength.Secondary の場合、ドライバーは基本文字、分音符号、その他の二次的な違い、および大文字と小文字を比較します。
- strength がその他の値の場合、この引数は無視されます。

この引数が false の場合、ドライバーは強度レベル Primary または Secondary での大文字と小文字の比較を含みません。

データ型: boolean
デフォルト: false

CaseLevel

caseFirst

(Optional) Specifies the sort order of case differences during tertiary level comparisons.

Data Type: CollationCaseFirst
Default: CollationCaseFirst.Off

CaseFirst

strength

(Optional) Specifies the level of comparison to perform, as defined in the ICU documentation.

Data Type: CollationStrength
Default: CollationStrength.Tertiary

Strength

numericOrdering

(任意)ドライバーが数字の string を数値として比較するかどうかを指定します。この引数が

true102の場合、ドライバーは数字の10 210

falsestring10 2を数値として比較します。例は、"" という文字列を比較する場合、および1 "``2 `` の場合、ドライバーは値を および として処理し、 が大きいことを検出します。この引数が または除外されている場合、ドライバーは数字の string を string として比較します。例は、"" という文字列を比較する場合、および "q" の場合、ドライバーは一度に 1 文字を 1 文字ずつ比較します。のため "" はが10 "q" より小さい場合、ドライバーは "q" を見つけます。は、"" より小さくなければなりません。詳細については、2

MongoDB Serverマニュアルの「 照合制限

」を参照してください。データ型:boolean
デフォルト:false

NumericOrdering

alternate

(Optional) Specifies whether the driver considers whitespace and punctuation as base characters for purposes of comparison.

Data Type: CollationAlternate
Default: CollationAlternate.NonIgnorable (spaces and punctuation are considered base characters)

Alternate

maxVariable

(Optional) Specifies which characters the driver considers ignorable when the alternate argument is CollationAlternate.Shifted.

Data Type: CollationMaxVariable
Default: CollationMaxVariable.Punctuation (the driver ignores punctuation and spaces)

MaxVariable

normalization

(Optional) Specifies whether the driver normalizes text as needed.

Most text doesn't require normalization. For more information about normalization, see the ICU documentation.

Data Type: boolean
Default: false

Normalization

backwards

(任意) 発音区別符号を含む string を、string の後ろから前にソートするかどうかを指定します。

データ型: boolean
デフォルト: false

Backwards

照合の詳細については、 MongoDB Serverマニュアルの 照合 ページを参照してください。

このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。