对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

检索字段的不同值

您可以使用 Distinct() 方法在集合中检索一个字段的不同值列表。

提示

参阅“使用示例”,了解如何运行此示例。

以下示例对 movies 集合执行以下操作:

  • 匹配 directors 中包含“Natalie 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()

给本页内容打分