Skip to content

Commit b466cba

Browse files
authored
DOCSP-51823 Standardize Distinct usage example (#543)
* DOCSP-51823 Standardize Distinct usage example * code comment fix * NR review * remaining review * tech feedback
1 parent 8be9d8e commit b466cba

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

source/crud/query/distinct.txt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,37 @@ 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+
This example performs the following actions on the ``restaurant``
127+
collection:
128+
129+
- Matches documents in which the value of the ``cuisine`` field is ``"Tapas"``
130+
- Returns distinct values of the ``borough`` field from the matched documents
131+
132+
.. io-code-block::
133+
:copyable: true
134+
135+
.. input:: /includes/usage-examples/code-snippets/distinct.go
136+
:language: go
137+
:dedent:
138+
139+
.. output::
140+
:language: none
141+
:visible: false
142+
143+
Brooklyn
144+
Manhattan
145+
Queens
146+
119147
Additional Information
120148
----------------------
121149

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

126152
API Documentation

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,24 @@ 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+
1626
func main() {
1727
if err := godotenv.Load(); err != nil {
1828
log.Println("No .env file found")
1929
}
2030

2131
var uri string
2232
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")
33+
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")
2434
}
2535

2636
client, err := mongo.Connect(options.Client().ApplyURI(uri))
@@ -33,25 +43,25 @@ func main() {
3343
}
3444
}()
3545

36-
// begin distinct
37-
coll := client.Database("sample_mflix").Collection("movies")
38-
filter := bson.D{{"directors", "Natalie Portman"}}
46+
// Filters the collection for documents where the value of cuisine is "Tapas"
47+
coll := client.Database("sample_restaurants").Collection("restaurants")
48+
filter := bson.D{{"cuisine", "Tapas"}}
3949

40-
// Retrieves the distinct values of the "title" field in documents
50+
// Retrieves the distinct values of the "borough" field in documents
4151
// that match the filter
4252
var arr []string
43-
err = coll.Distinct(context.TODO(), "title", filter).Decode(&arr)
53+
err = coll.Distinct(context.TODO(), "borough", filter).Decode(&arr)
4454
if err != nil {
4555
panic(err)
4656
}
47-
// end distinct
4857

49-
// Prints the distinct "title" values
58+
// Prints the distinct "borough" values
5059
for _, result := range arr {
5160
fmt.Println(result)
5261
}
5362

5463
// When you run this file, it should print:
55-
// A Tale of Love and Darkness
56-
// New York, I Love You
64+
// Brooklyn
65+
// Manhattan
66+
// Queens
5767
}

0 commit comments

Comments
 (0)