Docs Menu
Docs Home
/ / /
Go Driver
/

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

Distinct()メソッドを使用して、コレクション全体のフィールドの個別の値のリストを取得できます。

Tip

この例の実行方法については、「 使用例」をお読みください。

次の例では、movies コレクションに対して次の操作を実行します。

  • directorsに「Netalie Portman」が含まれているドキュメントと一致します

  • 一致したドキュメントからtitleの個別の値を返します

coll := client.Database("sample_mflix").Collection("movies")
filter := bson.D{{"directors", "Natalie Portman"}}
// Retrieves the distinct values of the "title" field in documents
// that match the filter
results, err := coll.Distinct(context.TODO(), "title", filter)
// Prints a message if any errors occur during the operation
if err != nil {
panic(err)
}

が 完全に実行可能な例 を表示

完全な例を実行すると、次の値を含むinterfaceタイプの空のスライスが返されます。

A Tale of Love and Darkness
New York, I Love You

個別の値を取得する方法の詳細については、「個別の値を取得する 」を参照してください。

distinct()

戻る

ドキュメントをカウント