Docs Menu
Docs Home
/ /

個別のフィールド値の一覧表示

Collectionインスタンスで distinct() メソッドを呼び出すと、コレクション内のドキュメントフィールドの個別の値を一覧表示できます。例、コレクション内のドキュメントに dateフィールドが含まれている場合、distinct() メソッドを使用して、コレクション内のそのフィールドに可能なすべての値を検索できます。

フィールド名をパラメーターとしてdistinct()メソッドに渡すと、そのフィールドの個別の値が返されます。 また、パラメーターとしてクエリフィルターを渡し、一致したドキュメントのサブセットのみから個別のフィールド値を検索することもできます。 クエリフィルターの作成の詳細については、「 クエリの指定」ガイドを参照してください。

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

この例では、 sample_restaurantsデータベースのrestaurantsコレクション内のフィールドの個別の値を検索します。

この例では、 cuisineフィールドの値が"Turkish"であるドキュメントのサブセット内で、 boroughフィールドの個別の値を検索します。

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

use std::env;
use mongodb::{
bson::{ Document, doc },
Client,
Collection };
#[tokio::main]
async fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri).await?;
let my_coll: Collection<Document> = client
.database("sample_restaurants")
.collection("restaurants");
let filter = doc! { "cuisine": "Turkish" };
let boroughs = my_coll.distinct("borough", filter, None).await?;
println!("List of field values for 'borough':");
for b in boroughs.iter() {
println!("{:?}", b);
}
Ok(())
}
List of field values for 'borough':
String("Brooklyn")
String("Manhattan")
String("Queens")
String("Staten Island")
use std::env;
use mongodb::{
bson::{ Document, doc },
sync::{ Client, Collection }
};
fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri)?;
let my_coll: Collection<Document> = client
.database("sample_restaurants")
.collection("restaurants");
let filter = doc! { "cuisine": "Turkish" };
let boroughs = my_coll.distinct("borough", filter, None)?;
println!("List of field values for 'borough':");
for b in boroughs.iter() {
println!("{:?}", b);
}
Ok(())
}
List of field values for 'borough':
String("Brooklyn")
String("Manhattan")
String("Queens")
String("Staten Island")

戻る