Skip to content

Commit c9765c3

Browse files
authored
DOCSP-26434: use struct in specify query pg (#210)
1 parent 3609061 commit c9765c3

File tree

2 files changed

+105
-84
lines changed

2 files changed

+105
-84
lines changed

source/fundamentals/crud/read-operations/query-document.txt

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ method to match a subset of documents.
5252
Sample Data
5353
~~~~~~~~~~~
5454

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+
5567
To run the examples in this guide, load the sample data into the
5668
``tea.ratings`` collection with the following
5769
snippet:
@@ -64,12 +76,10 @@ snippet:
6476

6577
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
6678

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
6981
``vendor`` fields.
7082

71-
.. include:: /includes/fundamentals/truncated-id.rst
72-
7383
.. _golang-literal-values:
7484

7585
Literal Values
@@ -95,25 +105,26 @@ The following example matches documents where the ``type`` is "Oolong":
95105
:language: go
96106

97107
filter := bson.D{{"type", "Oolong"}}
98-
108+
99109
cursor, err := coll.Find(context.TODO(), filter)
100110
if err != nil {
101-
panic(err)
111+
panic(err)
102112
}
103-
104-
var results []bson.D
113+
114+
var results []Tea
105115
if err = cursor.All(context.TODO(), &results); err != nil {
106-
panic(err)
116+
panic(err)
107117
}
108118
for _, result := range results {
109-
fmt.Println(result)
119+
res, _ := json.Marshal(result)
120+
fmt.Println(string(res))
110121
}
111122

112123
.. output::
113124
:language: none
114125
:visible: false
115126

116-
[{_id ObjectID("...")} {type Oolong} {rating 7} {vendor [C]}]
127+
{"Type":"Oolong","Rating":7,"Vendor":["C"]}
117128

118129
.. tip::
119130

@@ -158,20 +169,21 @@ than ``7``:
158169
panic(err)
159170
}
160171

161-
var results []bson.D
172+
var results []Tea
162173
if err = cursor.All(context.TODO(), &results); err != nil {
163-
panic(err)
174+
panic(err)
164175
}
165176
for _, result := range results {
166-
fmt.Println(result)
177+
res, _ := json.Marshal(result)
178+
fmt.Println(string(res))
167179
}
168180

169181
.. output::
170182
:language: none
171183
:visible: false
172184

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}
175187

176188
For a full list of comparison operators, see the :manual:`Comparison
177189
Query Operators </reference/operator/query-comparison/>` page.
@@ -211,20 +223,21 @@ than ``7`` and less than or equal to ``10``:
211223
panic(err)
212224
}
213225

214-
var results []bson.D
226+
var results []Tea
215227
if err = cursor.All(context.TODO(), &results); err != nil {
216-
panic(err)
228+
panic(err)
217229
}
218230
for _, result := range results {
219-
fmt.Println(result)
231+
res, _ := json.Marshal(result)
232+
fmt.Println(string(res))
220233
}
221234

222235
.. output::
223236
:language: none
224237
:visible: false
225238

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"]}
228241

229242
For a full list of logical operators, see the :manual:`Logical
230243
Query Operators </reference/operator/query-logical/>` page.
@@ -273,20 +286,21 @@ not exist:
273286
panic(err)
274287
}
275288

276-
var results []bson.D
289+
var results []Tea
277290
if err = cursor.All(context.TODO(), &results); err != nil {
278-
panic(err)
291+
panic(err)
279292
}
280293
for _, result := range results {
281-
fmt.Println(result)
294+
res, _ := json.Marshal(result)
295+
fmt.Println(string(res))
282296
}
283297

284298
.. output::
285299
:language: none
286300
:visible: false
287301

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}
290304

291305
For a full list of element operators, see the :manual:`Element
292306
Query Operators </reference/operator/query-element/>` page.
@@ -319,20 +333,21 @@ the letter "E":
319333
panic(err)
320334
}
321335

322-
var results []bson.D
336+
var results []Tea
323337
if err = cursor.All(context.TODO(), &results); err != nil {
324-
panic(err)
338+
panic(err)
325339
}
326340
for _, result := range results {
327-
fmt.Println(result)
341+
res, _ := json.Marshal(result)
342+
fmt.Println(string(res))
328343
}
329344

330345
.. output::
331346
:language: none
332347
:visible: false
333348

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"]}
336351

337352
For a full list of evaluation operators, see the :manual:`Evaluation
338353
Query Operators </reference/operator/query-evaluation/>` page.
@@ -360,20 +375,21 @@ The following example matches documents where the ``vendor`` contains "C":
360375
panic(err)
361376
}
362377

363-
var results []bson.D
378+
var results []Tea
364379
if err = cursor.All(context.TODO(), &results); err != nil {
365-
panic(err)
380+
panic(err)
366381
}
367382
for _, result := range results {
368-
fmt.Println(result)
383+
res, _ := json.Marshal(result)
384+
fmt.Println(string(res))
369385
}
370386

371387
.. output::
372388
:language: none
373389
:visible: false
374390

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"]}
377393

378394
For a full list of array operators, see the :manual:`Array
379395
Query Operators </reference/operator/query-array/>` page.
@@ -405,20 +421,21 @@ bits set as ``6`` (which is "00000110"):
405421
panic(err)
406422
}
407423

408-
var results []bson.D
424+
var results []Tea
409425
if err = cursor.All(context.TODO(), &results); err != nil {
410-
panic(err)
426+
panic(err)
411427
}
412428
for _, result := range results {
413-
fmt.Println(result)
429+
res, _ := json.Marshal(result)
430+
fmt.Println(string(res))
414431
}
415432

416433
.. output::
417434
:language: none
418435
:visible: false
419436

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"]}
422439

423440
For a full list of bitwise operators, see the :manual:`Bitwise
424441
Query Operators </reference/operator/query-bitwise/>` page.

0 commit comments

Comments
 (0)