Retrieve Distinct Values of a Field
This version of the documentation is archived and no longer supported. View the current documentation to learn how to upgrade your version of the MongoDB Go Driver.
You can retrieve a list of distinct values for a field across a
collection by using the Distinct()
method.
Example
Tip
Read the Usage Examples to learn how to run this example.
The following example performs the following on the movies
collection:
Matches documents in which the
directors
contains "Natalie Portman"Returns distinct values of the
title
from the matched documents
coll := client.Database("sample_mflix").Collection("movies") filter := bson.D{{"directors", "Natalie Portman"}} results, err := coll.Distinct(context.TODO(), "title", filter) if err != nil { panic(err) }
View a fully runnable example
Expected Result
After you run the full example, it returns an empty slice of an
interface
type that contains the following values:
A Tale of Love and Darkness New York, I Love You
Additional Information
To learn more about retrieving distinct values, see Retrieve Distinct Values.