@@ -26,8 +26,17 @@ Read operations allow you to do the following:
26
26
Sample Data
27
27
~~~~~~~~~~~
28
28
29
+ The examples in this section use the following ``Review`` struct as a model for documents
30
+ in the ``reviews`` collection:
31
+
32
+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/retrieve.go
33
+ :start-after: start-review-struct
34
+ :end-before: end-review-struct
35
+ :language: go
36
+ :dedent:
37
+
29
38
To run the examples in this guide, load these documents into the
30
- ``tea.ratings `` collection with the following
39
+ ``tea.reviews `` collection with the following
31
40
snippet:
32
41
33
42
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/retrieve.go
@@ -36,7 +45,11 @@ snippet:
36
45
:start-after: begin insert docs
37
46
:end-before: end insert docs
38
47
39
- .. include:: /includes/fundamentals/tea-sample-data-ending.rst
48
+ .. include:: /includes/fundamentals/automatic-db-coll-creation.rst
49
+
50
+ Each document describes the tea variety a customer ordered, their
51
+ rating, and the date of the order. These descriptions correspond to the
52
+ ``item``, ``rating``, and ``date_ordered`` fields.
40
53
41
54
.. _golang-retrieve-find:
42
55
@@ -109,14 +122,15 @@ following methods:
109
122
- | The field and type of sort to order the matched documents. You can specify an ascending or descending sort.
110
123
| Default: none
111
124
112
- Example
113
- ```````
125
+ Find Example
126
+ ````````````
114
127
115
128
The following example passes a context, filter, and ``FindOptions`` to
116
129
the ``Find()`` method, which performs the following actions:
117
130
118
- - Matches documents where the ``rating`` falls between ``5`` and ``10``
119
- - Returns the ``type`` and ``rating``, but excludes the ``_id``
131
+ - Matches documents where the ``rating`` value is between ``5`` and
132
+ ``9`` (exclusive)
133
+ - Sorts matched documents in ascending order by ``date_ordered``
120
134
121
135
.. io-code-block::
122
136
:copyable: true
@@ -125,69 +139,69 @@ the ``Find()`` method, which performs the following actions:
125
139
:language: go
126
140
127
141
filter := bson.D{
128
- {"$and",
129
- bson.A{
130
- bson.D{{"rating", bson.D{{"$gt", 5}}}},
131
- bson.D{{"rating", bson.D{{"$lt", 10 }}}},
132
- }},
142
+ {"$and",
143
+ bson.A{
144
+ bson.D{{"rating", bson.D{{"$gt", 5}}}},
145
+ bson.D{{"rating", bson.D{{"$lt", 9 }}}},
146
+ }},
133
147
}
134
- projection := bson.D{{"type ", 1}, {"rating", 1}, {"_id", 0 }}
135
- opts := options.Find().SetProjection(projection )
136
-
148
+ sort := bson.D{{"date_ordered ", 1}}
149
+ opts := options.Find().SetSort(sort )
150
+
137
151
cursor, err := coll.Find(context.TODO(), filter, opts)
138
152
if err != nil {
139
- panic(err)
153
+ panic(err)
140
154
}
141
-
142
- var results []bson.D
155
+
156
+ var results []Review
143
157
if err = cursor.All(context.TODO(), &results); err != nil {
144
- panic(err)
158
+ panic(err)
145
159
}
146
160
for _, result := range results {
147
- fmt.Println(result)
161
+ res, _ := json.Marshal(result)
162
+ fmt.Println(string(res))
148
163
}
149
164
150
165
.. output::
151
166
:language: none
152
167
:visible: false
153
168
154
- [{type Masala} {rating 7}]
155
- [{type Earl Grey} {rating 9}]
169
+ {"Item":"Sencha","Rating":7,"DateOrdered":"2009-11-18T05:00:00Z"}
170
+ {"Item":"Masala","Rating":8,"DateOrdered":"2009-12-01T05:00:00Z"}
156
171
157
- Example
158
- ```````
172
+ Find One Example
173
+ ````````````````
159
174
160
175
The following example passes a context, filter, and ``FindOneOptions``
161
176
to the ``FindOne()`` method, which performs the following actions:
162
177
163
- - Matches all documents
164
- - A descending sort on the ``rating`` field
165
- - Returns the ``type`` and ``rating``, but excludes the ``_id``
178
+ - Matches documents where the ``date_ordered`` value is on or before November
179
+ 30, 2009
180
+ - Skips the first two matched documents
166
181
167
182
.. io-code-block::
168
183
:copyable: true
169
184
170
185
.. input::
171
186
:language: go
172
187
173
- filter := bson.D{}
174
- sort := bson.D{{"rating", -1}}
175
- projection := bson.D{{"type", 1}, {"rating", 1}, {"_id", 0}}
176
- opts := options.FindOne().SetSort(sort).SetProjection(projection)
177
-
178
- var result bson.D
188
+ filter := bson.D{{"date_ordered", bson.D{{"$lte", time.Date(2009, 11, 30, 0, 0, 0, 0, time.Local)}}}}
189
+ opts := options.FindOne().SetSkip(2)
190
+
191
+ var result Review
179
192
err := coll.FindOne(context.TODO(), filter, opts).Decode(&result)
180
193
if err != nil {
181
- panic(err)
194
+ panic(err)
182
195
}
183
-
184
- fmt.Println(result)
196
+
197
+ res, _ := json.Marshal(result)
198
+ fmt.Println(string(res))
185
199
186
200
.. output::
187
201
:language: none
188
202
:visible: false
189
203
190
- [{type Masala} {rating 10}]
204
+ {"Item":" Masala","Rating":9,"DateOrdered":"2009-11-12T05:00:00Z"}
191
205
192
206
.. _golang-retrieve-aggregation:
193
207
@@ -272,8 +286,8 @@ Example
272
286
The following example passes a context and an aggregation pipeline that
273
287
performs the following actions:
274
288
275
- - Groups reviews by types
276
- - Calculates the average rating of each type
289
+ - Groups reviews by item ordered
290
+ - Calculates the average rating for each item
277
291
278
292
.. io-code-block::
279
293
:copyable: true
@@ -283,7 +297,7 @@ performs the following actions:
283
297
284
298
groupStage := bson.D{
285
299
{"$group", bson.D{
286
- {"_id", "$type "},
300
+ {"_id", "$item "},
287
301
{"average", bson.D{
288
302
{"$avg", "$rating"},
289
303
}},
@@ -299,15 +313,16 @@ performs the following actions:
299
313
panic(err)
300
314
}
301
315
for _, result := range results {
302
- fmt.Printf("%v has an average rating of %v \n", result["_id"], result["average"])
316
+ fmt.Printf("%v had an average rating of %v \n", result["_id"], result["average"])
303
317
}
304
318
305
319
.. output::
306
320
:language: none
307
321
:visible: false
308
322
309
- Masala has an average rating of 8.5
310
- Earl Grey has an average rating of 7
323
+ Sencha had an average rating of 8.5
324
+ Hibiscus had an average rating of 4
325
+ Masala had an average rating of 9
311
326
312
327
To learn more about how to construct an aggregation pipeline, see
313
328
the MongoDB server manual page on :manual:`Aggregation
0 commit comments