“文档” 菜单
文档首页
/ / /
Go 驱动程序
/

检索字段的不同值

您可以使用 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 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()

← 计算文档