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 filterresults, err := coll.Distinct(context.TODO(), "title", filter)
// Prints a message if any errors occur during the operationif err != nil { panic(err)}期待される結果
完全な例を実行すると、次の値を含むinterfaceタイプの空のスライスが返されます。
A Tale of Love and Darkness New York, I Love You詳細情報
個別の値を取得する方法の詳細については、「個別の値を取得する 」を参照してください。