@@ -52,6 +52,18 @@ method to match a subset of documents.
52
52
Sample Data
53
53
~~~~~~~~~~~
54
54
55
+ The examples in this section use the following ``Tea`` struct as a model for documents
56
+ in the ``ratings`` collection:
57
+
58
+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/query.go
59
+ :start-after: start-tea-struct
60
+ :end-before: end-tea-struct
61
+ :language: go
62
+ :dedent:
63
+
64
+ The ``omitempty`` :ref:`struct tag<golang-struct-tags>` omits the corresponding
65
+ field from the inserted document when left empty.
66
+
55
67
To run the examples in this guide, load the sample data into the
56
68
``tea.ratings`` collection with the following
57
69
snippet:
@@ -64,12 +76,10 @@ snippet:
64
76
65
77
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
66
78
67
- Each document contains a rating for a type of tea and the vendors that
68
- carry them, which corresponds to the ``rating ``, ``type ``, and
79
+ Each document describes a tea type, its rating, and the vendors that
80
+ carry that variety. These items correspond to the ``type ``, ``rating ``, and
69
81
``vendor`` fields.
70
82
71
- .. include:: /includes/fundamentals/truncated-id.rst
72
-
73
83
.. _golang-literal-values:
74
84
75
85
Literal Values
@@ -95,25 +105,26 @@ The following example matches documents where the ``type`` is "Oolong":
95
105
:language: go
96
106
97
107
filter := bson.D{{"type", "Oolong"}}
98
-
108
+
99
109
cursor, err := coll.Find(context.TODO(), filter)
100
110
if err != nil {
101
- panic(err)
111
+ panic(err)
102
112
}
103
-
104
- var results []bson.D
113
+
114
+ var results []Tea
105
115
if err = cursor.All(context.TODO(), &results); err != nil {
106
- panic(err)
116
+ panic(err)
107
117
}
108
118
for _, result := range results {
109
- fmt.Println(result)
119
+ res, _ := json.Marshal(result)
120
+ fmt.Println(string(res))
110
121
}
111
122
112
123
.. output::
113
124
:language: none
114
125
:visible: false
115
126
116
- [{_id ObjectID("...")} {type Oolong} {rating 7} {vendor [C]}]
127
+ {"Type":" Oolong","Rating":7,"Vendor":["C"]}
117
128
118
129
.. tip::
119
130
@@ -158,20 +169,21 @@ than ``7``:
158
169
panic(err)
159
170
}
160
171
161
- var results []bson.D
172
+ var results []Tea
162
173
if err = cursor.All(context.TODO(), &results); err != nil {
163
- panic(err)
174
+ panic(err)
164
175
}
165
176
for _, result := range results {
166
- fmt.Println(result)
177
+ res, _ := json.Marshal(result)
178
+ fmt.Println(string(res))
167
179
}
168
180
169
181
.. output::
170
182
:language: none
171
183
:visible: false
172
184
173
- [{_id ObjectID("...")} {type English Breakfast} {rating 6}]
174
- [{_id ObjectID("...")} {type Assam} {rating 5}]
185
+ {"Type":" English Breakfast","Rating":6}
186
+ {"Type":" Assam","Rating":5}
175
187
176
188
For a full list of comparison operators, see the :manual:`Comparison
177
189
Query Operators </reference/operator/query-comparison/>` page.
@@ -211,20 +223,21 @@ than ``7`` and less than or equal to ``10``:
211
223
panic(err)
212
224
}
213
225
214
- var results []bson.D
226
+ var results []Tea
215
227
if err = cursor.All(context.TODO(), &results); err != nil {
216
- panic(err)
228
+ panic(err)
217
229
}
218
230
for _, result := range results {
219
- fmt.Println(result)
231
+ res, _ := json.Marshal(result)
232
+ fmt.Println(string(res))
220
233
}
221
234
222
235
.. output::
223
236
:language: none
224
237
:visible: false
225
238
226
- [{_id ObjectID("...")} {type Masala} {rating 10} { vendor [A C]}]
227
- [{_id ObjectID("...")} {type Earl Grey} {rating 8} { vendor [A B]}]
239
+ {"Type":" Masala","Rating":10," vendor":["A","C"]}
240
+ {"Type":" Earl Grey","Rating":8," vendor":["A","B"]}
228
241
229
242
For a full list of logical operators, see the :manual:`Logical
230
243
Query Operators </reference/operator/query-logical/>` page.
@@ -273,20 +286,21 @@ not exist:
273
286
panic(err)
274
287
}
275
288
276
- var results []bson.D
289
+ var results []Tea
277
290
if err = cursor.All(context.TODO(), &results); err != nil {
278
- panic(err)
291
+ panic(err)
279
292
}
280
293
for _, result := range results {
281
- fmt.Println(result)
294
+ res, _ := json.Marshal(result)
295
+ fmt.Println(string(res))
282
296
}
283
297
284
298
.. output::
285
299
:language: none
286
300
:visible: false
287
301
288
- [{_id ObjectID("...")} {type English Breakfast} {rating 6}]
289
- [{_id ObjectID("...")} {type Assam} {rating 5}]
302
+ {"Type":" English Breakfast","Rating":6}
303
+ {"Type":" Assam","Rating":5}
290
304
291
305
For a full list of element operators, see the :manual:`Element
292
306
Query Operators </reference/operator/query-element/>` page.
@@ -319,20 +333,21 @@ the letter "E":
319
333
panic(err)
320
334
}
321
335
322
- var results []bson.D
336
+ var results []Tea
323
337
if err = cursor.All(context.TODO(), &results); err != nil {
324
- panic(err)
338
+ panic(err)
325
339
}
326
340
for _, result := range results {
327
- fmt.Println(result)
341
+ res, _ := json.Marshal(result)
342
+ fmt.Println(string(res))
328
343
}
329
344
330
345
.. output::
331
346
:language: none
332
347
:visible: false
333
348
334
- [{_id ObjectID("...")} {type English Breakfast} {rating 6}]
335
- [{_id ObjectID("...")} {type Earl Grey} {rating 8} { vendor [A B]}]
349
+ {"Type":" English Breakfast","Rating":6}
350
+ {"Type":" Earl Grey","Rating":8," vendor":["A","B"]}
336
351
337
352
For a full list of evaluation operators, see the :manual:`Evaluation
338
353
Query Operators </reference/operator/query-evaluation/>` page.
@@ -360,20 +375,21 @@ The following example matches documents where the ``vendor`` contains "C":
360
375
panic(err)
361
376
}
362
377
363
- var results []bson.D
378
+ var results []Tea
364
379
if err = cursor.All(context.TODO(), &results); err != nil {
365
- panic(err)
380
+ panic(err)
366
381
}
367
382
for _, result := range results {
368
- fmt.Println(result)
383
+ res, _ := json.Marshal(result)
384
+ fmt.Println(string(res))
369
385
}
370
386
371
387
.. output::
372
388
:language: none
373
389
:visible: false
374
390
375
- [{_id ObjectID("...")} {type Masala} {rating 10} { vendor [A C]}]
376
- [{_id ObjectID("...")} {type Oolong} {rating 7} { vendor [C]}]
391
+ {"Type":" Masala","Rating":10," vendor":["A","C"]}
392
+ {"Type":" Oolong","Rating":7," vendor":["C"]}
377
393
378
394
For a full list of array operators, see the :manual:`Array
379
395
Query Operators </reference/operator/query-array/>` page.
@@ -405,20 +421,21 @@ bits set as ``6`` (which is "00000110"):
405
421
panic(err)
406
422
}
407
423
408
- var results []bson.D
424
+ var results []Tea
409
425
if err = cursor.All(context.TODO(), &results); err != nil {
410
- panic(err)
426
+ panic(err)
411
427
}
412
428
for _, result := range results {
413
- fmt.Println(result)
429
+ res, _ := json.Marshal(result)
430
+ fmt.Println(string(res))
414
431
}
415
432
416
433
.. output::
417
434
:language: none
418
435
:visible: false
419
436
420
- [{_id ObjectID("...")} {type English Breakfast} {rating 6}]
421
- [{_id ObjectID("...")} {type Oolong} {rating 7} { vendor [C]}]
437
+ {"Type":" English Breakfast","Rating":6}
438
+ {"Type":" Oolong","Rating":7," vendor":["C"]}
422
439
423
440
For a full list of bitwise operators, see the :manual:`Bitwise
424
441
Query Operators </reference/operator/query-bitwise/>` page.
0 commit comments