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

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

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

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

distinct()

このページを評価