@@ -28,13 +28,13 @@ their related reference and API documentation.
2828 .. input::
2929 :language: go
3030
31- err = coll.FindOne(context.TODO(), bson.D{{"rating ", 5 }}).Decode(&result)
31+ err = coll.FindOne(context.TODO(), bson.D{{"firstName ", Mike }}).Decode(&result)
3232
3333 .. output::
3434 :language: go
3535 :visible: false
3636
37- [{type Assam }, {rating 5 } ...]
37+ [{firstName Mike }, {lastName Smith } ...]
3838
3939 * - | **Find Multiple Documents**
4040 |
@@ -48,14 +48,14 @@ their related reference and API documentation.
4848 .. input::
4949 :language: go
5050
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 }}}})
5252
5353 .. output::
5454 :language: go
5555 :visible: false
5656
57- [{type Masala }, {rating 10 }, ... ]
58- [{type Earl Grey }, {rating 8 }, ... ]
57+ [{firstName Kyle }, {age 51 }, ... ]
58+ [{firstName Omar }, {age 47 }, ... ]
5959
6060 * - | **Insert a Document**
6161 |
@@ -69,9 +69,8 @@ their related reference and API documentation.
6969 result, err := coll.InsertOne(
7070 context.TODO(),
7171 bson.D{
72- {"type", "Masala"},
73- {"rating", 10},
74- {"vendor", bson.A{"A", "C"}}
72+ {"animal", "Dog"},
73+ {"breed", "Beagle"}
7574 }
7675 )
7776
@@ -85,10 +84,10 @@ their related reference and API documentation.
8584 :copyable: true
8685
8786 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"}}
9291 }
9392
9493 result, err := coll.InsertMany(context.TODO(), docs)
@@ -107,15 +106,16 @@ their related reference and API documentation.
107106
108107 result, err := coll.UpdateOne(
109108 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 }}}}
112111 )
112+ fmt.Printf("The number of modified documents: %d\n", result.ModifiedCount)
113113
114114 .. output::
115115 :language: go
116116 :visible: false
117117
118- [{type Oolong}, {rating: 8} ...}]
118+ The number of modified documents: 1
119119
120120 * - | **Update Multiple Documents**
121121 |
@@ -131,17 +131,16 @@ their related reference and API documentation.
131131
132132 result, err := coll.UpdateMany(
133133 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" }}}}
136136 )
137+ fmt.Printf("The number of modified documents: %d\n", result.ModifiedCount)
137138
138139 .. output::
139140 :language: go
140141 :visible: false
141142
142- [{type English Breakfast}, {rating 8}, ... ]
143- [{type Oolong}, {rating 9}, ... ]
144- ...
143+ The number of modified documents: 4
145144
146145 * - | **Update Arrays in Documents**
147146 |
@@ -157,15 +156,15 @@ their related reference and API documentation.
157156 result, err := coll.UpdateMany(
158157 context.TODO(),
159158 bson.D{},
160- bson.D{{"$push", bson.D{{"vendor" , "D "}}}}
159+ bson.D{{"$push", bson.D{{family , "brother "}}}}
161160 )
162161
163162 .. output::
164163 :language: go
165164 :visible: false
166165
167- [{type English Breakfast }, {vendor ["D "]}, ... ]
168- [{type Oolong }, {vendor ["C ", "D "]}, ... ]
166+ [{firstName Xiao }, {family ["brother "]}, ... ]
167+ [{firstName Omar }, {family ["brother ", "mother "]}, ... ]
169168 ...
170169
171170 * - | **Replace a Document**
@@ -182,15 +181,15 @@ their related reference and API documentation.
182181
183182 result, err := coll.ReplaceOne(
184183 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" }}
187186 )
188187
189188 .. output::
190189 :language: go
191190 :visible: false
192191
193- [{type Jasmine }, {rating 9}, ... }]
192+ [{{firstName Mike }, {lastName Doe} }]
194193
195194 * - | **Delete a Document**
196195 |
@@ -203,7 +202,7 @@ their related reference and API documentation.
203202
204203 result, err := coll.DeleteOne(
205204 context.TODO(),
206- bson.D{{"type ", "Earl Grey "}}
205+ bson.D{{"firstName ", "Xiao "}}
207206 )
208207
209208 * - | **Delete Multiple Documents**
@@ -217,7 +216,7 @@ their related reference and API documentation.
217216
218217 results, err := coll.DeleteMany(
219218 context.TODO(),
220- bson.D{{"rating ", bson.D{{"$gt ", 7 }}}}
219+ bson.D{{"age ", bson.D{{"$lte ", 12 }}}}
221220 )
222221
223222 * - | **Bulk Write**
@@ -233,9 +232,9 @@ their related reference and API documentation.
233232 :language: go
234233
235234 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 }}}}),
239238 }
240239 opts := options.BulkWrite().SetOrdered(true)
241240
@@ -245,8 +244,8 @@ their related reference and API documentation.
245244 :language: go
246245 :visible: false
247246
248- [{type Chrysanthemum }, {rating 5} ... ]
249- [{type Oolong }, ... ]
247+ [{firstName John }, {age 5} ... ]
248+ [{firstName Juan }, {age 12} ... ]
250249
251250 * - | **Monitor Data Changes**
252251 |
@@ -284,9 +283,9 @@ their related reference and API documentation.
284283 :language: go
285284 :visible: false
286285
287- [{type Masala } ... ]
288- [{type English Breakfast } ...]
289- [{type Oolong } ...]
286+ [{firstName Doug } ... ]
287+ [{firstName Erik } ...]
288+ [{lastName Chang } ...]
290289 ...
291290
292291 * - | **Access Data from a Cursor as an Array**
@@ -311,9 +310,9 @@ their related reference and API documentation.
311310 :language: go
312311 :visible: false
313312
314- [{type Masala } ... ]
315- [{type English Breakfast } ...]
316- [{type Oolong } ...]
313+ [{name Mike } ... ]
314+ [{name Edgar } ...]
315+ [{name Freddie } ...]
317316 ...
318317
319318 * - | **Count Documents**
@@ -347,14 +346,14 @@ their related reference and API documentation.
347346 .. input::
348347 :language: go
349348
350- results, err := coll.Distinct(context.TODO(), "type ", bson.D{})
349+ results, err := coll.Distinct(context.TODO(), "firstName ", bson.D{})
351350
352351 .. output::
353352 :language: go
354353 :visible: false
355354
356- Masala
357- Oolong
355+ Mike
356+ Xiao
358357 ...
359358
360359 * - | **Limit the Number of Documents Retrieved**
@@ -374,8 +373,8 @@ their related reference and API documentation.
374373 :language: go
375374 :visible: false
376375
377- [{type Masala } ... ]
378- [{type English Breakfast } ...]
376+ [{breed Beagle } ... ]
377+ [{breed German Shepard } ...]
379378
380379 * - | **Skip Retrieved Documents**
381380 |
@@ -395,8 +394,8 @@ their related reference and API documentation.
395394 :language: go
396395 :visible: false
397396
398- [{type Earl Grey } ... ]
399- [{type Chrysanthemum } ...]
397+ [{item Pen } ... ]
398+ [{item Chair } ...]
400399
401400 * - | **Sort the Documents When Retrieving Them**
402401 |
@@ -409,15 +408,15 @@ their related reference and API documentation.
409408 .. input::
410409 :language: go
411410
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}}))
413412
414413 .. output::
415414 :language: go
416415 :visible: false
417416
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} ... ]
421420
422421 * - | **Project Document Fields When Retrieving Them**
423422 |
@@ -434,16 +433,16 @@ their related reference and API documentation.
434433 context.TODO(),
435434 bson.D{},
436435 options.Find().SetProjection(
437- bson.D{{"vendor ", 0}, {"_id",0}}
436+ bson.D{{"age ", 0}, {"_id",0}}
438437 )
439438 )
440439
441440 .. output::
442441 :language: go
443442 :visible: false
444443
445- [{type Masala} {rating 10 }]
446- [{type English Breakfast } {rating 8 }]
444+ [{firstName Lester }]
445+ [{firstName Wendall } {lastName Griffin }]
447446 ...
448447
449448 * - | **Create an Index**
@@ -454,7 +453,7 @@ their related reference and API documentation.
454453 - .. code-block:: go
455454 :copyable: true
456455
457- model := mongo.IndexModel{Keys: bson.D{{"type ", 1}, {"rating ", -1}}}
456+ model := mongo.IndexModel{Keys: bson.D{{"firstName ", 1}, {"lastName ", -1}}}
458457 name, err := coll.Indexes().CreateOne(context.TODO(), model)
459458
460459 * - | **Search Text**
@@ -469,10 +468,10 @@ their related reference and API documentation.
469468 :language: go
470469
471470 // 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 "}}}})
473472
474473 .. output::
475474 :language: go
476475 :visible: false
477476
478- [{type English Breakfast} {rating 8 } ... ]
477+ [{"firstName": "Emily" , "Description": "I love to play sports and walk my beagle." } ... ]
0 commit comments