Skip to content

Commit 90a1a69

Browse files
authored
DOCSP-26438: use struct in distinct pg (#213)
* DOCSP-26438: use struct in distinct pg * wording fix and add filter
1 parent 83952bd commit 90a1a69

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

source/fundamentals/crud/read-operations/distinct.txt

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ specified field across a single collection.
2121
Sample Data
2222
~~~~~~~~~~~
2323

24-
To run the examples in this guide, load the sample data into the
25-
``tea.ratings`` collection with the following
24+
The example in this guide uses the following ``Course`` struct as a model for documents
25+
in the ``courses`` collection:
26+
27+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/distinctValues.go
28+
:start-after: start-course-struct
29+
:end-before: end-course-struct
30+
:language: go
31+
:dedent:
32+
33+
To run the example, load the sample data into the
34+
``db.courses`` collection with the following
2635
snippet:
2736

2837
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/distinctValues.go
@@ -33,8 +42,10 @@ snippet:
3342

3443
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
3544

36-
Each document contains a rating for a type of tea that corresponds to
37-
the ``type`` and ``rating`` fields.
45+
Each document contains a description of a university course that
46+
includes the course title, department, and enrollment. These items
47+
correspond to the ``title``, ``department``, and ``enrollment`` fields
48+
in each document.
3849

3950
Distinct
4051
--------
@@ -48,8 +59,8 @@ method:
4859

4960
.. tip::
5061

51-
If you specify an empty query filter, the method matches all the
52-
documents in a collection.
62+
If you specify an empty query filter, the ``Distinct()`` method
63+
searches for distinct values across all documents in a collection.
5364

5465
Modify Behavior
5566
~~~~~~~~~~~~~~~
@@ -80,32 +91,31 @@ with the following methods:
8091
Example
8192
```````
8293

83-
The following example matches all documents and prints the distinct values
84-
of the ``type`` field using the ``Distinct()`` method:
94+
The following example matches documents with an ``enrollment`` field
95+
value less than ``50`` and prints the distinct values
96+
of the ``department`` field using the ``Distinct()`` method:
8597

8698
.. io-code-block::
8799
:copyable: true
88100

89101
.. input::
90102
:language: go
91103

92-
results, err := coll.Distinct(context.TODO(), "type", bson.D{})
104+
results, err := coll.Distinct(context.TODO(), "department", bson.D{{"enrollment", bson.D{{"$lt", 50}}}})
93105
if err != nil {
94-
panic(err)
106+
panic(err)
95107
}
96-
108+
97109
for _, result := range results {
98-
fmt.Println(result)
110+
fmt.Println(result)
99111
}
100112

101113
.. output::
102114
:language: none
103115
:visible: false
104116

105-
Earl Grey
106-
Masala
107-
Matcha
108-
Oolong
117+
Earth Science
118+
English
109119

110120
Additional Information
111121
----------------------

source/includes/fundamentals/code-snippets/CRUD/distinctValues.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import (
1111
"go.mongodb.org/mongo-driver/mongo/options"
1212
)
1313

14+
// start-course-struct
15+
type Course struct {
16+
Title string
17+
Department string
18+
Enrollment int32
19+
}
20+
21+
// end-course-struct
22+
1423
func main() {
1524
var uri string
1625
if uri = os.Getenv("DRIVER_REF_URI"); uri == "" {
@@ -27,31 +36,25 @@ func main() {
2736
}
2837
}()
2938

30-
client.Database("tea").Collection("ratings").Drop(context.TODO())
31-
3239
// begin insert docs
33-
coll := client.Database("tea").Collection("ratings")
40+
coll := client.Database("db").Collection("courses")
3441
docs := []interface{}{
35-
bson.D{{"type", "Masala"}, {"rating", 10}},
36-
bson.D{{"type", "Matcha"}, {"rating", 7}},
37-
bson.D{{"type", "Masala"}, {"rating", 4}},
38-
bson.D{{"type", "Oolong"}, {"rating", 9}},
39-
bson.D{{"type", "Matcha"}, {"rating", 5}},
40-
bson.D{{"type", "Earl Grey"}, {"rating", 8}},
41-
bson.D{{"type", "Oolong"}, {"rating", 3}},
42-
bson.D{{"type", "Matcha"}, {"rating", 6}},
43-
bson.D{{"type", "Earl Grey"}, {"rating", 4}},
42+
Course{Title: "World Fiction", Department: "English", Enrollment: 35},
43+
Course{Title: "Abstract Algebra", Department: "Mathematics", Enrollment: 60},
44+
Course{Title: "Modern Poetry", Department: "English", Enrollment: 12},
45+
Course{Title: "Plate Tectonics", Department: "Earth Science", Enrollment: 30},
4446
}
4547

4648
result, err := coll.InsertMany(context.TODO(), docs)
49+
//end insert docs
50+
4751
if err != nil {
4852
panic(err)
4953
}
5054
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
51-
//end insert docs
5255

5356
// begin distinct
54-
results, err := coll.Distinct(context.TODO(), "type", bson.D{})
57+
results, err := coll.Distinct(context.TODO(), "department", bson.D{{"enrollment", bson.D{{"$lt", 50}}}})
5558
if err != nil {
5659
panic(err)
5760
}

0 commit comments

Comments
 (0)