1+ // Performs a bulk write that performs write operations
12package main
23
34import (
@@ -52,13 +53,16 @@ func main() {
5253
5354 fmt .Println ("\n InsertOneModel:\n " )
5455 {
56+ // Creates instructions to insert documents describing books
5557 // begin bulk insert model
5658 models := []mongo.WriteModel {
5759 mongo .NewInsertOneModel ().SetDocument (Book {Title : "Beloved" , Author : "Toni Morrison" , Length : 324 }),
5860 mongo .NewInsertOneModel ().SetDocument (Book {Title : "Outline" , Author : "Rachel Cusk" , Length : 258 }),
5961 }
6062 // end bulk insert model
6163
64+ // Runs the bulk write operation and prints the number of
65+ // inserted documents
6266 results , err := coll .BulkWrite (context .TODO (), models )
6367 if err != nil {
6468 panic (err )
@@ -87,13 +91,16 @@ func main() {
8791
8892 fmt .Println ("\n ReplaceOneModel:\n " )
8993 {
94+ // Creates instructions to replace the first matching document
9095 // begin bulk replace model
9196 models := []mongo.WriteModel {
9297 mongo .NewReplaceOneModel ().SetFilter (bson.D {{"title" , "Lucy" }}).
9398 SetReplacement (Book {Title : "On Beauty" , Author : "Zadie Smith" , Length : 473 }),
9499 }
95100 // end bulk replace model
96101
102+ // Runs the bulk write operation and prints the number of
103+ // replaced documents
97104 results , err := coll .BulkWrite (context .TODO (), models )
98105 if err != nil {
99106 panic (err )
@@ -122,13 +129,16 @@ func main() {
122129
123130 fmt .Println ("\n UpdateOneModel:\n " )
124131 {
132+ // Creates instructions to update the first matching document
125133 // begin bulk update model
126134 models := []mongo.WriteModel {
127135 mongo .NewUpdateOneModel ().SetFilter (bson.D {{"author" , "Elena Ferrante" }}).
128136 SetUpdate (bson.D {{"$inc" , bson.D {{"length" , - 15 }}}}),
129137 }
130138 // end bulk update model
131139
140+ // Runs the bulk write operation and prints the number of
141+ // updated documents
132142 results , err := coll .BulkWrite (context .TODO (), models )
133143 if err != nil {
134144 panic (err )
@@ -157,12 +167,15 @@ func main() {
157167
158168 fmt .Println ("\n DeleteManyModel:\n " )
159169 {
170+ // Creates instructions to delete all documents that match the filter
160171 // begin bulk delete model
161172 models := []mongo.WriteModel {
162173 mongo .NewDeleteManyModel ().SetFilter (bson.D {{"length" , bson.D {{"$gt" , 300 }}}}),
163174 }
164175 // end bulk delete model
165176
177+ // Runs the bulk write operation and prints the number of
178+ // deleted documents
166179 results , err := coll .BulkWrite (context .TODO (), models )
167180 if err != nil {
168181 panic (err )
@@ -208,6 +221,8 @@ func main() {
208221
209222 fmt .Println ("\n BulkOperation Example:\n " )
210223 {
224+ // Creates instructions to make changes to documents describing
225+ // books
211226 // begin unordered
212227 models := []mongo.WriteModel {
213228 mongo .NewInsertOneModel ().SetDocument (Book {Title : "Middlemarch" , Author : "George Eliot" , Length : 904 }),
@@ -218,8 +233,12 @@ func main() {
218233 SetUpdate (bson.D {{"$inc" , bson.D {{"length" , 10 }}}}),
219234 mongo .NewDeleteManyModel ().SetFilter (bson.D {{"author" , bson.D {{"$regex" , "Jam" }}}}),
220235 }
236+
237+ // Specifies that the bulk write is unordered
221238 opts := options .BulkWrite ().SetOrdered (false )
222239
240+ // Runs the bulk write operation and prints a summary of the
241+ // data changes
223242 results , err := coll .BulkWrite (context .TODO (), models , opts )
224243 if err != nil {
225244 panic (err )
0 commit comments