From a589d71a7470effb07b539a525d4c0a85afce8e3 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Mon, 21 Jul 2025 19:14:51 -0400 Subject: [PATCH 1/5] DOCSP-51823 Standardize Distinct usage example --- source/crud/query/distinct.txt | 61 ++++++++++++++++++- .../usage-examples/code-snippets/distinct.go | 35 ++++++++--- .../code-snippets/distinctBsonD.go | 57 +++++++++++++++++ 3 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 source/includes/usage-examples/code-snippets/distinctBsonD.go diff --git a/source/crud/query/distinct.txt b/source/crud/query/distinct.txt index 36d599e9..7880c6a3 100644 --- a/source/crud/query/distinct.txt +++ b/source/crud/query/distinct.txt @@ -116,11 +116,68 @@ 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 + +The following example performs the following actions on the ``restaurant`` +collection: + +- Matches documents in which the value of ``cuisine`` is ``"Tapas"`` +- Returns distinct values of the ``borough`` field from the matched documents + +.. tabs:: + + .. tab:: Struct + :tabid: structExample + + The following code uses a struct to retrieve distinct values of the + ``borough`` field for documents in the ``restaurants`` collection that match + the specified filter: + + .. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/distinct.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Brooklyn + Manhattan + Queens + + .. tab:: bson.D + :tabid: bsonDExample + + The following code uses a bson.D type to retrieve distinct values of the + ``borough`` field for documents in the ``restaurants`` collection that match + the specified filter: + + .. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/distinctBsonD.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..f8543486 100644 --- a/source/includes/usage-examples/code-snippets/distinct.go +++ b/source/includes/usage-examples/code-snippets/distinct.go @@ -13,6 +13,21 @@ 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{} +} + +// Creates a filter struct to use for the query +type RestaurantCuisineFilter struct { + Cuisine string +} + func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -20,7 +35,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 +48,25 @@ func main() { } }() - // begin distinct - coll := client.Database("sample_mflix").Collection("movies") - filter := bson.D{{"directors", "Natalie Portman"}} + // Filter the collection for documents where the value of cuisine is "Tapas" + coll := client.Database("sample_restaurants").Collection("restaurants") + filter := RestaurantCuisineFilter{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 } diff --git a/source/includes/usage-examples/code-snippets/distinctBsonD.go b/source/includes/usage-examples/code-snippets/distinctBsonD.go new file mode 100644 index 00000000..8fd32ea5 --- /dev/null +++ b/source/includes/usage-examples/code-snippets/distinctBsonD.go @@ -0,0 +1,57 @@ +// Retrieves distinct values of a field by using the Go driver +package main + +import ( + "context" + "fmt" + "log" + "os" + + "github.com/joho/godotenv" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" +) + +func main() { + if err := godotenv.Load(); err != nil { + log.Println("No .env file found") + } + + 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/usage-examples/#environment-variable") + } + + client, err := mongo.Connect(options.Client().ApplyURI(uri)) + if err != nil { + panic(err) + } + defer func() { + if err = client.Disconnect(context.TODO()); err != nil { + panic(err) + } + }() + + // Filter 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 "borough" field in documents + // that match the filter + var arr []string + err = coll.Distinct(context.TODO(), "borough", filter).Decode(&arr) + if err != nil { + panic(err) + } + + // Prints the distinct "borough" values + for _, result := range arr { + fmt.Println(result) + } + + // When you run this file, it should print: + // Brooklyn + // Manhattan + // Queens +} From 6e206e9d9f441825c546fcc12e55093b87e4855f Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Mon, 21 Jul 2025 19:39:26 -0400 Subject: [PATCH 2/5] code comment fix --- source/includes/usage-examples/code-snippets/distinct.go | 2 +- source/includes/usage-examples/code-snippets/distinctBsonD.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/includes/usage-examples/code-snippets/distinct.go b/source/includes/usage-examples/code-snippets/distinct.go index f8543486..4adc2788 100644 --- a/source/includes/usage-examples/code-snippets/distinct.go +++ b/source/includes/usage-examples/code-snippets/distinct.go @@ -48,7 +48,7 @@ func main() { } }() - // Filter the collection for documents where the value of cuisine is "Tapas" + // Filters the collection for documents where the value of cuisine is "Tapas" coll := client.Database("sample_restaurants").Collection("restaurants") filter := RestaurantCuisineFilter{Cuisine: "Tapas"} diff --git a/source/includes/usage-examples/code-snippets/distinctBsonD.go b/source/includes/usage-examples/code-snippets/distinctBsonD.go index 8fd32ea5..ed408c7a 100644 --- a/source/includes/usage-examples/code-snippets/distinctBsonD.go +++ b/source/includes/usage-examples/code-snippets/distinctBsonD.go @@ -33,7 +33,7 @@ func main() { } }() - // Filter the collection for documents where the value of cuisine is "Tapas" + // Filters the collection for documents where the value of cuisine is "Tapas" coll := client.Database("sample_restaurants").Collection("restaurants") filter := bson.D{{"cuisine", "Tapas"}} From 5341816eb4d5dd5aa0447fe01d122a72747a434d Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Wed, 23 Jul 2025 14:48:29 -0400 Subject: [PATCH 3/5] NR review --- source/crud/query/distinct.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/crud/query/distinct.txt b/source/crud/query/distinct.txt index 7880c6a3..2027d9d4 100644 --- a/source/crud/query/distinct.txt +++ b/source/crud/query/distinct.txt @@ -123,7 +123,7 @@ Retrieve Distinct Values Example: Full File .. include:: /includes/usage-examples/example-intro.rst -The following example performs the following actions on the ``restaurant`` +This example performs the following actions on the ``restaurant`` collection: - Matches documents in which the value of ``cuisine`` is ``"Tapas"`` From 729a723cf0234dc7bcd8b2a1325e713419e08b7d Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Wed, 23 Jul 2025 14:49:07 -0400 Subject: [PATCH 4/5] remaining review --- source/crud/query/distinct.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/crud/query/distinct.txt b/source/crud/query/distinct.txt index 2027d9d4..444ef069 100644 --- a/source/crud/query/distinct.txt +++ b/source/crud/query/distinct.txt @@ -126,7 +126,7 @@ Retrieve Distinct Values Example: Full File This example performs the following actions on the ``restaurant`` collection: -- Matches documents in which the value of ``cuisine`` is ``"Tapas"`` +- Matches documents in which the value of the ``cuisine`` field is ``"Tapas"`` - Returns distinct values of the ``borough`` field from the matched documents .. tabs:: From b5f5aaf77fbcf2056bb86ef00810657d465c7702 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Fri, 25 Jul 2025 17:58:57 -0400 Subject: [PATCH 5/5] tech feedback --- source/crud/query/distinct.txt | 53 ++++------------- .../usage-examples/code-snippets/distinct.go | 7 +-- .../code-snippets/distinctBsonD.go | 57 ------------------- 3 files changed, 12 insertions(+), 105 deletions(-) delete mode 100644 source/includes/usage-examples/code-snippets/distinctBsonD.go diff --git a/source/crud/query/distinct.txt b/source/crud/query/distinct.txt index 444ef069..59332cc5 100644 --- a/source/crud/query/distinct.txt +++ b/source/crud/query/distinct.txt @@ -129,51 +129,20 @@ collection: - Matches documents in which the value of the ``cuisine`` field is ``"Tapas"`` - Returns distinct values of the ``borough`` field from the matched documents -.. tabs:: - - .. tab:: Struct - :tabid: structExample - - The following code uses a struct to retrieve distinct values of the - ``borough`` field for documents in the ``restaurants`` collection that match - the specified filter: - - .. io-code-block:: - :copyable: true - - .. input:: /includes/usage-examples/code-snippets/distinct.go - :language: go - :dedent: - - .. output:: - :language: none - :visible: false - - Brooklyn - Manhattan - Queens - - .. tab:: bson.D - :tabid: bsonDExample - - The following code uses a bson.D type to retrieve distinct values of the - ``borough`` field for documents in the ``restaurants`` collection that match - the specified filter: - - .. io-code-block:: - :copyable: true +.. io-code-block:: + :copyable: true - .. input:: /includes/usage-examples/code-snippets/distinctBsonD.go - :language: go - :dedent: + .. input:: /includes/usage-examples/code-snippets/distinct.go + :language: go + :dedent: - .. output:: - :language: none - :visible: false + .. output:: + :language: none + :visible: false - Brooklyn - Manhattan - Queens + Brooklyn + Manhattan + Queens Additional Information ---------------------- diff --git a/source/includes/usage-examples/code-snippets/distinct.go b/source/includes/usage-examples/code-snippets/distinct.go index 4adc2788..7adbfb8a 100644 --- a/source/includes/usage-examples/code-snippets/distinct.go +++ b/source/includes/usage-examples/code-snippets/distinct.go @@ -23,11 +23,6 @@ type Restaurant struct { Grades interface{} } -// Creates a filter struct to use for the query -type RestaurantCuisineFilter struct { - Cuisine string -} - func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -50,7 +45,7 @@ func main() { // Filters the collection for documents where the value of cuisine is "Tapas" coll := client.Database("sample_restaurants").Collection("restaurants") - filter := RestaurantCuisineFilter{Cuisine: "Tapas"} + filter := bson.D{{"cuisine", "Tapas"}} // Retrieves the distinct values of the "borough" field in documents // that match the filter diff --git a/source/includes/usage-examples/code-snippets/distinctBsonD.go b/source/includes/usage-examples/code-snippets/distinctBsonD.go deleted file mode 100644 index ed408c7a..00000000 --- a/source/includes/usage-examples/code-snippets/distinctBsonD.go +++ /dev/null @@ -1,57 +0,0 @@ -// Retrieves distinct values of a field by using the Go driver -package main - -import ( - "context" - "fmt" - "log" - "os" - - "github.com/joho/godotenv" - "go.mongodb.org/mongo-driver/v2/bson" - "go.mongodb.org/mongo-driver/v2/mongo" - "go.mongodb.org/mongo-driver/v2/mongo/options" -) - -func main() { - if err := godotenv.Load(); err != nil { - log.Println("No .env file found") - } - - 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/usage-examples/#environment-variable") - } - - client, err := mongo.Connect(options.Client().ApplyURI(uri)) - if err != nil { - panic(err) - } - defer func() { - if err = client.Disconnect(context.TODO()); err != nil { - panic(err) - } - }() - - // 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 "borough" field in documents - // that match the filter - var arr []string - err = coll.Distinct(context.TODO(), "borough", filter).Decode(&arr) - if err != nil { - panic(err) - } - - // Prints the distinct "borough" values - for _, result := range arr { - fmt.Println(result) - } - - // When you run this file, it should print: - // Brooklyn - // Manhattan - // Queens -}