@@ -28,13 +28,13 @@ their related reference and API documentation.
28
28
.. input::
29
29
:language: go
30
30
31
- err = coll.FindOne(context.TODO(), bson.D{{"rating ", 5 }}).Decode(&result)
31
+ err = coll.FindOne(context.TODO(), bson.D{{"firstName ", Mike }}).Decode(&result)
32
32
33
33
.. output::
34
34
:language: go
35
35
:visible: false
36
36
37
- [{type Assam }, {rating 5 } ...]
37
+ [{firstName Mike }, {lastName Smith } ...]
38
38
39
39
* - | **Find Multiple Documents**
40
40
|
@@ -48,14 +48,14 @@ their related reference and API documentation.
48
48
.. input::
49
49
:language: go
50
50
51
- cursor, err := coll.Find(context.TODO(), bson.D{{"rating ", bson.D{{"$gte", 8 }}}})
51
+ cursor, err := coll.Find(context.TODO(), bson.D{{"age ", bson.D{{"$gte", 46 }}}})
52
52
53
53
.. output::
54
54
:language: go
55
55
:visible: false
56
56
57
- [{type Masala }, {rating 10 }, ... ]
58
- [{type Earl Grey }, {rating 8 }, ... ]
57
+ [{firstName Kyle }, {age 51 }, ... ]
58
+ [{firstName Omar }, {age 47 }, ... ]
59
59
60
60
* - | **Insert a Document**
61
61
|
@@ -69,9 +69,8 @@ their related reference and API documentation.
69
69
result, err := coll.InsertOne(
70
70
context.TODO(),
71
71
bson.D{
72
- {"type", "Masala"},
73
- {"rating", 10},
74
- {"vendor", bson.A{"A", "C"}}
72
+ {"animal", "Dog"},
73
+ {"breed", "Beagle"}
75
74
}
76
75
)
77
76
@@ -85,10 +84,10 @@ their related reference and API documentation.
85
84
:copyable: true
86
85
87
86
docs := []interface{} {
88
- bson.D{{"type ", "English Breakfast "}, {"rating ", 6 }},
89
- bson.D{{"type ", "Oolong "}, {"rating ", 7 }, {"vendor ", bson.A{"C"} }},
90
- bson.D{{"type ", "Assam"}, {"rating", 5 }},
91
- bson.D{{"type ", "Earl Grey "}, {"rating ", 8}, {"vendor", bson.A{"A", "B"}}},
87
+ bson.D{{"firstName ", "Erik "}, {"age ", 27 }},
88
+ bson.D{{"firstName ", "Mohammad "}, {"lastName ", "Ahmad" }, {"age ", 10 }},
89
+ bson.D{{"firstName ", "Todd" }},
90
+ bson.D{{"firstName ", "Juan "}, {"lastName ", "Pablo"}}
92
91
}
93
92
94
93
result, err := coll.InsertMany(context.TODO(), docs)
@@ -107,15 +106,16 @@ their related reference and API documentation.
107
106
108
107
result, err := coll.UpdateOne(
109
108
context.TODO(),
110
- bson.D{{"type ", "Oolong "}},
111
- bson.D{{"$set", bson.D{{"rating ", 8 }}}}
109
+ bson.D{{"firstName ", "Erik "}},
110
+ bson.D{{"$set", bson.D{{"age ", 28 }}}}
112
111
)
112
+ fmt.Printf("The number of modified documents: %d\n", result.ModifiedCount)
113
113
114
114
.. output::
115
115
:language: go
116
116
:visible: false
117
117
118
- [{type Oolong}, {rating: 8} ...}]
118
+ The number of modified documents: 1
119
119
120
120
* - | **Update Multiple Documents**
121
121
|
@@ -131,17 +131,16 @@ their related reference and API documentation.
131
131
132
132
result, err := coll.UpdateMany(
133
133
context.TODO(),
134
- bson.D{{"rating ", bson.D{{"$lt ", 10 }}}},
135
- bson.D{{"$inc ", bson.D{{"rating ", 2 }}}}
134
+ bson.D{{"age ", bson.D{{"$gte ", 58 }}}},
135
+ bson.D{{"$set ", bson.D{{"description ", "Senior" }}}}
136
136
)
137
+ fmt.Printf("The number of modified documents: %d\n", result.ModifiedCount)
137
138
138
139
.. output::
139
140
:language: go
140
141
:visible: false
141
142
142
- [{type English Breakfast}, {rating 8}, ... ]
143
- [{type Oolong}, {rating 9}, ... ]
144
- ...
143
+ The number of modified documents: 4
145
144
146
145
* - | **Update Arrays in Documents**
147
146
|
@@ -157,15 +156,15 @@ their related reference and API documentation.
157
156
result, err := coll.UpdateMany(
158
157
context.TODO(),
159
158
bson.D{},
160
- bson.D{{"$push", bson.D{{"vendor" , "D "}}}}
159
+ bson.D{{"$push", bson.D{{family , "brother "}}}}
161
160
)
162
161
163
162
.. output::
164
163
:language: go
165
164
:visible: false
166
165
167
- [{type English Breakfast }, {vendor ["D "]}, ... ]
168
- [{type Oolong }, {vendor ["C ", "D "]}, ... ]
166
+ [{firstName Xiao }, {family ["brother "]}, ... ]
167
+ [{firstName Omar }, {family ["brother ", "mother "]}, ... ]
169
168
...
170
169
171
170
* - | **Replace a Document**
@@ -182,15 +181,15 @@ their related reference and API documentation.
182
181
183
182
result, err := coll.ReplaceOne(
184
183
context.TODO(),
185
- bson.D{{"type ", "Oolong "}},
186
- bson.D{{"type ", "Jasmine "}, {"rating ", 9 }}
184
+ bson.D{{"firstName ", "Mick "}},
185
+ bson.D{{"firstName ", "Mike "}, {"lastName ", "Doe" }}
187
186
)
188
187
189
188
.. output::
190
189
:language: go
191
190
:visible: false
192
191
193
- [{type Jasmine }, {rating 9}, ... }]
192
+ [{{firstName Mike }, {lastName Doe} }]
194
193
195
194
* - | **Delete a Document**
196
195
|
@@ -203,7 +202,7 @@ their related reference and API documentation.
203
202
204
203
result, err := coll.DeleteOne(
205
204
context.TODO(),
206
- bson.D{{"type ", "Earl Grey "}}
205
+ bson.D{{"firstName ", "Xiao "}}
207
206
)
208
207
209
208
* - | **Delete Multiple Documents**
@@ -217,7 +216,7 @@ their related reference and API documentation.
217
216
218
217
results, err := coll.DeleteMany(
219
218
context.TODO(),
220
- bson.D{{"rating ", bson.D{{"$gt ", 7 }}}}
219
+ bson.D{{"age ", bson.D{{"$lte ", 12 }}}}
221
220
)
222
221
223
222
* - | **Bulk Write**
@@ -233,9 +232,9 @@ their related reference and API documentation.
233
232
:language: go
234
233
235
234
models := []mongo.WriteModel{
236
- mongo.NewInsertOneModel().SetDocument(bson.D{{"type ", "Chrysanthemum "}, {"rating ", 5}}),
237
- mongo.NewUpdateOneModel().SetFilter(bson.D{{"type ", "Jasmine "}}).
238
- SetUpdate(bson.D{{"$set", bson.D{{"type ", "Oolong" }}}}),
235
+ mongo.NewInsertOneModel().SetDocument(bson.D{{"firstName ", "John "}, {"age ", 5}}),
236
+ mongo.NewUpdateOneModel().SetFilter(bson.D{{"firstName ", "Juan "}}).
237
+ SetUpdate(bson.D{{"$set", bson.D{{"age ", 12 }}}}),
239
238
}
240
239
opts := options.BulkWrite().SetOrdered(true)
241
240
@@ -245,8 +244,8 @@ their related reference and API documentation.
245
244
:language: go
246
245
:visible: false
247
246
248
- [{type Chrysanthemum }, {rating 5} ... ]
249
- [{type Oolong }, ... ]
247
+ [{firstName John }, {age 5} ... ]
248
+ [{firstName Juan }, {age 12} ... ]
250
249
251
250
* - | **Monitor Data Changes**
252
251
|
@@ -284,9 +283,9 @@ their related reference and API documentation.
284
283
:language: go
285
284
:visible: false
286
285
287
- [{type Masala } ... ]
288
- [{type English Breakfast } ...]
289
- [{type Oolong } ...]
286
+ [{firstName Doug } ... ]
287
+ [{firstName Erik } ...]
288
+ [{lastName Chang } ...]
290
289
...
291
290
292
291
* - | **Access Data from a Cursor as an Array**
@@ -311,9 +310,9 @@ their related reference and API documentation.
311
310
:language: go
312
311
:visible: false
313
312
314
- [{type Masala } ... ]
315
- [{type English Breakfast } ...]
316
- [{type Oolong } ...]
313
+ [{name Mike } ... ]
314
+ [{name Edgar } ...]
315
+ [{name Freddie } ...]
317
316
...
318
317
319
318
* - | **Count Documents**
@@ -347,14 +346,14 @@ their related reference and API documentation.
347
346
.. input::
348
347
:language: go
349
348
350
- results, err := coll.Distinct(context.TODO(), "type ", bson.D{})
349
+ results, err := coll.Distinct(context.TODO(), "firstName ", bson.D{})
351
350
352
351
.. output::
353
352
:language: go
354
353
:visible: false
355
354
356
- Masala
357
- Oolong
355
+ Mike
356
+ Xiao
358
357
...
359
358
360
359
* - | **Limit the Number of Documents Retrieved**
@@ -374,8 +373,8 @@ their related reference and API documentation.
374
373
:language: go
375
374
:visible: false
376
375
377
- [{type Masala } ... ]
378
- [{type English Breakfast } ...]
376
+ [{breed Beagle } ... ]
377
+ [{breed German Shepard } ...]
379
378
380
379
* - | **Skip Retrieved Documents**
381
380
|
@@ -395,8 +394,8 @@ their related reference and API documentation.
395
394
:language: go
396
395
:visible: false
397
396
398
- [{type Earl Grey } ... ]
399
- [{type Chrysanthemum } ...]
397
+ [{item Pen } ... ]
398
+ [{item Chair } ...]
400
399
401
400
* - | **Sort the Documents When Retrieving Them**
402
401
|
@@ -409,15 +408,15 @@ their related reference and API documentation.
409
408
.. input::
410
409
:language: go
411
410
412
- cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSort(bson.D{{"rating ", 1}}))
411
+ cursor, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetSort(bson.D{{"age ", 1}}))
413
412
414
413
.. output::
415
414
:language: go
416
415
:visible: false
417
416
418
- [{type Chrysanthemum } {rating 5} ... ]
419
- [{type Assam } {rating 7} ... ]
420
- [{type English Breakfast } {rating 8} ... ]
417
+ [{firstName Dev } {age 5} ... ]
418
+ [{firstName Jose } {age 7} ... ]
419
+ [{firstName Om } {age 8} ... ]
421
420
422
421
* - | **Project Document Fields When Retrieving Them**
423
422
|
@@ -434,16 +433,16 @@ their related reference and API documentation.
434
433
context.TODO(),
435
434
bson.D{},
436
435
options.Find().SetProjection(
437
- bson.D{{"vendor ", 0}, {"_id",0}}
436
+ bson.D{{"age ", 0}, {"_id",0}}
438
437
)
439
438
)
440
439
441
440
.. output::
442
441
:language: go
443
442
:visible: false
444
443
445
- [{type Masala} {rating 10 }]
446
- [{type English Breakfast } {rating 8 }]
444
+ [{firstName Lester }]
445
+ [{firstName Wendall } {lastName Griffin }]
447
446
...
448
447
449
448
* - | **Create an Index**
@@ -454,7 +453,7 @@ their related reference and API documentation.
454
453
- .. code-block:: go
455
454
:copyable: true
456
455
457
- model := mongo.IndexModel{Keys: bson.D{{"type ", 1}, {"rating ", -1}}}
456
+ model := mongo.IndexModel{Keys: bson.D{{"firstName ", 1}, {"lastName ", -1}}}
458
457
name, err := coll.Indexes().CreateOne(context.TODO(), model)
459
458
460
459
* - | **Search Text**
@@ -469,10 +468,10 @@ their related reference and API documentation.
469
468
:language: go
470
469
471
470
// only searches fields with text indexes
472
- cursor, err := coll.Find(context.TODO(), bson.D{{"$text", bson.D{{"$search", "Breakfast "}}}})
471
+ cursor, err := coll.Find(context.TODO(), bson.D{{"$text", bson.D{{"$search", "beagle "}}}})
473
472
474
473
.. output::
475
474
:language: go
476
475
:visible: false
477
476
478
- [{type English Breakfast} {rating 8 } ... ]
477
+ [{"firstName": "Emily" , "Description": "I love to play sports and walk my beagle." } ... ]
0 commit comments