Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

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

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

コレクション内では、ドキュメントには単一のフィールドに異なる値が含まれる場合があります。例、restaurantsコレクション内の 1 つのドキュメントの borough 値は "Manhattan" で、別のドキュメントの borough 値は "Queens" です。 Rustドライバーを使用して、"Manhattan""Queens"、およびその他すべての一意の値を restaurantsコレクション内のすべてのドキュメントにわたって boroughフィールドに取得できます。

このガイドの例では、Atlasサンプルデータセットsample_restaurantsデータベースのrestaurantsコレクションを使用します。MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、MongoDB Get Started ガイドを参照してください。

restaurantsコレクション内のドキュメントには、Document 型またはカスタムデータ型のインスタンスとしてアクセスできます。コレクションのデータを表すデータ型を指定するには、<T> 型パラメータを次のいずれかの値に置き換えます。

  • <Document>:コレクションドキュメントをBSONドキュメントとして表現します

  • <Restaurant>: コード例で定義されている Restaurant 構造体のインスタンスとしてコレクションドキュメントを表します

指定されたフィールドの個別の値を取得するには、 インスタンスで個別の() Collectionメソッドを呼び出し、個別の値を検索するフィールドの名前を渡します。

distinct()Vec<Bson>メソッドは、Bson 値のベクトルである オブジェクトとして個別の値のリストを返します。

次の例では、 restaurantsコレクション内の boroughフィールドの個別の値を取得します。

AsynchronousSynchronous各実行時に対応するコードを表示するには、 タブまたは タブを選択します。

let boroughs = my_coll.distinct("borough", None).await?;
println!("List of field values for 'borough':");
for b in boroughs {
println!("{:?}", b);
}
List of field values for 'borough':
String("Bronx")
String("Brooklyn")
String("Manhattan")
String("Missing")
String("Queens")
String("Staten Island")
let boroughs = my_coll.distinct("borough", None).run()?;
println!("List of field values for 'borough':");
for b in boroughs {
println!("{:?}", b);
}
List of field values for 'borough':
String("Bronx")
String("Brooklyn")
String("Manhattan")
String("Missing")
String("Queens")
String("Staten Island")

この操作では、個別の boroughフィールド値をそれぞれ保存するベクトルが返されます。 boroughフィールドでは複数のドキュメントが同じ値になっていますが、各値は結果に 1 回だけ表示されます。

distinct()メソッドにクエリフィルターを提供すると、コレクション内のドキュメントのサブセット全体で個別のフィールド値を検索できます。クエリフィルターは、操作内のドキュメントを一致させるために使用される検索条件を指定する式です。例、distinct() メソッドを使用すると、一致したドキュメントのサブセットのみから個別のフィールド値を検索できます。

Tip

クエリフィルターの作成の詳細については、クエリの指定ガイドを参照してください。

次の例では、cuisineフィールドの値が "Turkish" であるすべてのドキュメントの boroughフィールドの個別の値を取得します。

各実行時に対応するコードを表示するには、 AsynchronousタブまたはSynchronousタブを選択します。

let filter = doc! { "cuisine": "Turkish" };
let boroughs = my_coll.distinct("borough", filter).await?;
println!("List of field values for 'borough':");
for b in boroughs {
println!("{:?}", b);
}
List of field values for 'borough':
String("Brooklyn")
String("Manhattan")
String("Queens")
String("Staten Island")
let filter = doc! { "cuisine": "Turkish" };
let boroughs = my_coll.distinct("borough", filter).run()?;
println!("List of field values for 'borough':");
for b in boroughs {
println!("{:?}", b);
}
List of field values for 'borough':
String("Brooklyn")
String("Manhattan")
String("Queens")
String("Staten Island")

distinct() メソッドの動作を変更するには、await または run() メソッドを呼び出す前に、1 つ以上のオプション メソッドを count_documents() 呼び出しに連結します。次の表では、個別の操作をカスタマイズするために設定できるオプションについて説明します。

オプション
説明

collation()

The collation to use for the operation.
Type: Collation

hint()

The index to use for the operation.
Type: Hint

comment()

The comment to attach to the operation.
Type: Bson

max_time()

The maximum amount of time that the operation can run.
Type: Duration

read_concern()

The read concern to use for the operation.
Type: ReadConcern

selection_criteria()

The read preference and tags to use for the operation.
Type: SelectionCriteria

distinct() メソッドの詳細については、API ドキュメントをご覧ください。

戻る

ドキュメントをカウント

項目一覧