diff --git a/source/crud/query/distinct.txt b/source/crud/query/distinct.txt index 36d599e9..59332cc5 100644 --- a/source/crud/query/distinct.txt +++ b/source/crud/query/distinct.txt @@ -116,11 +116,37 @@ of the ``department`` field by using the ``Distinct()`` method: [English Geology] +.. _golang-distinct-usage-example: + +Retrieve Distinct Values Example: Full File +------------------------------------------- + +.. include:: /includes/usage-examples/example-intro.rst + +This example performs the following actions on the ``restaurant`` +collection: + +- Matches documents in which the value of the ``cuisine`` field is ``"Tapas"`` +- Returns distinct values of the ``borough`` field from the matched documents + +.. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/distinct.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Brooklyn + Manhattan + Queens + Additional Information ---------------------- -For a runnable example that retrieves distinct values, see :ref:`golang-distinct-usage-example`. - To learn about constructing a query filter, see :ref:`golang-query-document`. API Documentation diff --git a/source/includes/usage-examples/code-snippets/distinct.go b/source/includes/usage-examples/code-snippets/distinct.go index f3f0a6af..7adbfb8a 100644 --- a/source/includes/usage-examples/code-snippets/distinct.go +++ b/source/includes/usage-examples/code-snippets/distinct.go @@ -13,6 +13,16 @@ import ( "go.mongodb.org/mongo-driver/v2/mongo/options" ) +type Restaurant struct { + ID bson.ObjectID `bson:"_id"` + Name string + RestaurantId string `bson:"restaurant_id"` + Cuisine string + Address interface{} + Borough string + Grades interface{} +} + func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -20,7 +30,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) @@ -33,25 +43,25 @@ func main() { } }() - // begin distinct - coll := client.Database("sample_mflix").Collection("movies") - filter := bson.D{{"directors", "Natalie Portman"}} + // Filters the collection for documents where the value of cuisine is "Tapas" + coll := client.Database("sample_restaurants").Collection("restaurants") + filter := bson.D{{"cuisine", "Tapas"}} - // Retrieves the distinct values of the "title" field in documents + // Retrieves the distinct values of the "borough" field in documents // that match the filter var arr []string - err = coll.Distinct(context.TODO(), "title", filter).Decode(&arr) + err = coll.Distinct(context.TODO(), "borough", filter).Decode(&arr) if err != nil { panic(err) } - // end distinct - // Prints the distinct "title" values + // Prints the distinct "borough" values for _, result := range arr { fmt.Println(result) } // When you run this file, it should print: - // A Tale of Love and Darkness - // New York, I Love You + // Brooklyn + // Manhattan + // Queens }