Skip to content

Commit 285d228

Browse files
authored
DOCSP-26435: use struct in count documents pg (#211)
1 parent c9765c3 commit 285d228

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ the number of documents in your collection.
2222
Sample Data
2323
~~~~~~~~~~~
2424

25+
The examples in this section use the following ``Tea`` struct as a model for documents
26+
in the ``ratings`` collection:
27+
28+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/countAndEstimate.go
29+
:start-after: start-tea-struct
30+
:end-before: end-tea-struct
31+
:language: go
32+
:dedent:
33+
2534
To run the examples in this guide, load the sample data into the ``tea.ratings`` collection with the following
2635
snippet:
2736

@@ -33,8 +42,8 @@ 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 describes a tea type and its rating. These items
46+
correspond to the ``type`` and ``rating`` fields.
3847

3948
.. _golang-accurate-count:
4049

@@ -99,18 +108,18 @@ The following example counts the number of documents where the
99108
:language: go
100109

101110
filter := bson.D{{"rating", bson.D{{"$lt", 6}}}}
102-
111+
103112
count, err := coll.CountDocuments(context.TODO(), filter)
104113
if err != nil {
105-
panic(err)
114+
panic(err)
106115
}
107-
fmt.Printf("Number of ratings less than six: %d\n", count)
116+
fmt.Printf("Number of documents with a rating less than six: %d\n", count)
108117

109118
.. output::
110119
:language: none
111120
:visible: false
112121

113-
Number of ratings less than six: 4
122+
Number of documents with a rating less than six: 4
114123

115124
.. _golang-count-aggregation:
116125

@@ -126,7 +135,7 @@ Example
126135
The following example performs the following actions:
127136

128137
- Counts the number of documents where the ``rating`` is greater than ``5``
129-
- Assigns the count to a field called ``total_documents``
138+
- Assigns the count to the ``counted_documents`` field
130139

131140
.. io-code-block::
132141
:copyable: true
@@ -135,7 +144,7 @@ The following example performs the following actions:
135144
:language: go
136145

137146
matchStage := bson.D{{"$match", bson.D{{"rating", bson.D{{"$gt", 5}}}}}}
138-
countStage := bson.D{{"$count", "total_documents"}}
147+
countStage := bson.D{{"$count", "counted_documents"}}
139148

140149
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{matchStage, countStage})
141150
if err != nil {
@@ -154,7 +163,7 @@ The following example performs the following actions:
154163
:language: none
155164
:visible: false
156165

157-
[{total_documents 5}]
166+
[{counted_documents 5}]
158167

159168
.. _golang-estimated-count:
160169

@@ -191,7 +200,7 @@ options with the following methods:
191200
- | The maximum amount of time that the query can run on the server.
192201
| Default: ``nil``
193202

194-
Example
203+
Example
195204
```````
196205

197206
The following example estimates the number of documents in the

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

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

14+
// start-tea-struct
15+
type Tea struct {
16+
Type string
17+
Rating int32
18+
}
19+
20+
// end-tea-struct
21+
1422
func main() {
1523
var uri string
1624
if uri = os.Getenv("MONGODB_URI"); uri == "" {
@@ -27,28 +35,27 @@ func main() {
2735
}
2836
}()
2937

30-
client.Database("tea").Collection("ratings").Drop(context.TODO())
31-
3238
// begin insert docs
3339
coll := client.Database("tea").Collection("ratings")
3440
docs := []interface{}{
35-
bson.D{{"type", "Masala"}, {"rating", 10}},
36-
bson.D{{"type", "Matcha"}, {"rating", 7}},
37-
bson.D{{"type", "Assam"}, {"rating", 4}},
38-
bson.D{{"type", "Oolong"}, {"rating", 9}},
39-
bson.D{{"type", "Chrysanthemum"}, {"rating", 5}},
40-
bson.D{{"type", "Earl Grey"}, {"rating", 8}},
41-
bson.D{{"type", "Jasmine"}, {"rating", 3}},
42-
bson.D{{"type", "English Breakfast"}, {"rating", 6}},
43-
bson.D{{"type", "White Peony"}, {"rating", 4}},
41+
Tea{Type: "Masala", Rating: 10},
42+
Tea{Type: "Matcha", Rating: 7},
43+
Tea{Type: "Assam", Rating: 4},
44+
Tea{Type: "Oolong", Rating: 9},
45+
Tea{Type: "Chrysanthemum", Rating: 5},
46+
Tea{Type: "Earl Grey", Rating: 8},
47+
Tea{Type: "Jasmine", Rating: 3},
48+
Tea{Type: "English Breakfast", Rating: 6},
49+
Tea{Type: "White Peony", Rating: 4},
4450
}
4551

4652
result, err := coll.InsertMany(context.TODO(), docs)
53+
//end insert docs
54+
4755
if err != nil {
4856
panic(err)
4957
}
5058
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
51-
//end insert docs
5259

5360
{
5461
// begin count documents
@@ -58,7 +65,7 @@ func main() {
5865
if err != nil {
5966
panic(err)
6067
}
61-
fmt.Printf("Number of ratings less than six: %d\n", count)
68+
fmt.Printf("Number of documents with a rating less than six: %d\n", count)
6269
// end count documents
6370
}
6471

0 commit comments

Comments
 (0)