Skip to content

Commit a589d71

Browse files
committed
DOCSP-51823 Standardize Distinct usage example
1 parent 61b2f4c commit a589d71

File tree

3 files changed

+141
-12
lines changed

3 files changed

+141
-12
lines changed

source/crud/query/distinct.txt

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,68 @@ of the ``department`` field by using the ``Distinct()`` method:
116116

117117
[English Geology]
118118

119+
.. _golang-distinct-usage-example:
120+
121+
Retrieve Distinct Values Example: Full File
122+
-------------------------------------------
123+
124+
.. include:: /includes/usage-examples/example-intro.rst
125+
126+
The following example performs the following actions on the ``restaurant``
127+
collection:
128+
129+
- Matches documents in which the value of ``cuisine`` is ``"Tapas"``
130+
- Returns distinct values of the ``borough`` field from the matched documents
131+
132+
.. tabs::
133+
134+
.. tab:: Struct
135+
:tabid: structExample
136+
137+
The following code uses a struct to retrieve distinct values of the
138+
``borough`` field for documents in the ``restaurants`` collection that match
139+
the specified filter:
140+
141+
.. io-code-block::
142+
:copyable: true
143+
144+
.. input:: /includes/usage-examples/code-snippets/distinct.go
145+
:language: go
146+
:dedent:
147+
148+
.. output::
149+
:language: none
150+
:visible: false
151+
152+
Brooklyn
153+
Manhattan
154+
Queens
155+
156+
.. tab:: bson.D
157+
:tabid: bsonDExample
158+
159+
The following code uses a bson.D type to retrieve distinct values of the
160+
``borough`` field for documents in the ``restaurants`` collection that match
161+
the specified filter:
162+
163+
.. io-code-block::
164+
:copyable: true
165+
166+
.. input:: /includes/usage-examples/code-snippets/distinctBsonD.go
167+
:language: go
168+
:dedent:
169+
170+
.. output::
171+
:language: none
172+
:visible: false
173+
174+
Brooklyn
175+
Manhattan
176+
Queens
177+
119178
Additional Information
120179
----------------------
121180

122-
For a runnable example that retrieves distinct values, see :ref:`golang-distinct-usage-example`.
123-
124181
To learn about constructing a query filter, see :ref:`golang-query-document`.
125182

126183
API Documentation

source/includes/usage-examples/code-snippets/distinct.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,29 @@ import (
1313
"go.mongodb.org/mongo-driver/v2/mongo/options"
1414
)
1515

16+
type Restaurant struct {
17+
ID bson.ObjectID `bson:"_id"`
18+
Name string
19+
RestaurantId string `bson:"restaurant_id"`
20+
Cuisine string
21+
Address interface{}
22+
Borough string
23+
Grades interface{}
24+
}
25+
26+
// Creates a filter struct to use for the query
27+
type RestaurantCuisineFilter struct {
28+
Cuisine string
29+
}
30+
1631
func main() {
1732
if err := godotenv.Load(); err != nil {
1833
log.Println("No .env file found")
1934
}
2035

2136
var uri string
2237
if uri = os.Getenv("MONGODB_URI"); uri == "" {
23-
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")
38+
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")
2439
}
2540

2641
client, err := mongo.Connect(options.Client().ApplyURI(uri))
@@ -33,25 +48,25 @@ func main() {
3348
}
3449
}()
3550

36-
// begin distinct
37-
coll := client.Database("sample_mflix").Collection("movies")
38-
filter := bson.D{{"directors", "Natalie Portman"}}
51+
// Filter the collection for documents where the value of cuisine is "Tapas"
52+
coll := client.Database("sample_restaurants").Collection("restaurants")
53+
filter := RestaurantCuisineFilter{Cuisine: "Tapas"}
3954

40-
// Retrieves the distinct values of the "title" field in documents
55+
// Retrieves the distinct values of the "borough" field in documents
4156
// that match the filter
4257
var arr []string
43-
err = coll.Distinct(context.TODO(), "title", filter).Decode(&arr)
58+
err = coll.Distinct(context.TODO(), "borough", filter).Decode(&arr)
4459
if err != nil {
4560
panic(err)
4661
}
47-
// end distinct
4862

49-
// Prints the distinct "title" values
63+
// Prints the distinct "borough" values
5064
for _, result := range arr {
5165
fmt.Println(result)
5266
}
5367

5468
// When you run this file, it should print:
55-
// A Tale of Love and Darkness
56-
// New York, I Love You
69+
// Brooklyn
70+
// Manhattan
71+
// Queens
5772
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Retrieves distinct values of a field by using the Go driver
2+
package main
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"github.com/joho/godotenv"
11+
"go.mongodb.org/mongo-driver/v2/bson"
12+
"go.mongodb.org/mongo-driver/v2/mongo"
13+
"go.mongodb.org/mongo-driver/v2/mongo/options"
14+
)
15+
16+
func main() {
17+
if err := godotenv.Load(); err != nil {
18+
log.Println("No .env file found")
19+
}
20+
21+
var uri string
22+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
23+
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")
24+
}
25+
26+
client, err := mongo.Connect(options.Client().ApplyURI(uri))
27+
if err != nil {
28+
panic(err)
29+
}
30+
defer func() {
31+
if err = client.Disconnect(context.TODO()); err != nil {
32+
panic(err)
33+
}
34+
}()
35+
36+
// Filter the collection for documents where the value of cuisine is "Tapas"
37+
coll := client.Database("sample_restaurants").Collection("restaurants")
38+
filter := bson.D{{"cuisine", "Tapas"}}
39+
40+
// Retrieves the distinct values of the "borough" field in documents
41+
// that match the filter
42+
var arr []string
43+
err = coll.Distinct(context.TODO(), "borough", filter).Decode(&arr)
44+
if err != nil {
45+
panic(err)
46+
}
47+
48+
// Prints the distinct "borough" values
49+
for _, result := range arr {
50+
fmt.Println(result)
51+
}
52+
53+
// When you run this file, it should print:
54+
// Brooklyn
55+
// Manhattan
56+
// Queens
57+
}

0 commit comments

Comments
 (0)