forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud_examples_test.go
913 lines (781 loc) · 29.3 KB
/
crud_examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package mongo_test
import (
"context"
"fmt"
"log"
"sync"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// Client examples
func ExampleClient_ListDatabaseNames() {
var client *mongo.Client
// use a filter to only select non-empty databases
result, err := client.ListDatabaseNames(context.TODO(), bson.D{{"empty", false}})
if err != nil {
log.Fatal(err)
}
for _, db := range result {
fmt.Println(db)
}
}
func ExampleClient_Watch() {
var client *mongo.Client
// specify a pipeline that will only match "insert" events
// specify the MaxAwaitTimeOption to have each attempt wait two seconds for new documents
matchStage := bson.D{{"$match", bson.D{{"operationType", "insert"}}}}
opts := options.ChangeStream().SetMaxAwaitTime(2 * time.Second)
changeStream, err := client.Watch(context.TODO(), mongo.Pipeline{matchStage}, opts)
if err != nil {
log.Fatal(err)
}
// print out all change stream events in the order they're received
// see the mongo.ChangeStream documentation for more examples of using change streams
for changeStream.Next(context.TODO()) {
fmt.Println(changeStream.Current)
}
}
// Database examples
func ExampleDatabase_CreateCollection() {
var db *mongo.Database
// Create a "users" collection with a JSON schema validator. The validator will ensure that each document in the
// collection has "name" and "age" fields.
jsonSchema := bson.M{
"bsonType": "object",
"required": []string{"name", "age"},
"properties": bson.M{
"name": bson.M{
"bsonType": "string",
"description": "the name of the user, which is required and must be a string",
},
"age": bson.M{
"bsonType": "int",
"minimum": 18,
"description": "the age of the user, which is required and must be an integer >= 18",
},
},
}
validator := bson.M{
"$jsonSchema": jsonSchema,
}
opts := options.CreateCollection().SetValidator(validator)
if err := db.CreateCollection(context.TODO(), "users", opts); err != nil {
log.Fatal(err)
}
}
func ExampleDatabase_CreateView() {
var db *mongo.Database
// Create a view on the "users" collection called "usernames". Specify a pipeline that concatenates the "firstName"
// and "lastName" fields from each document in "users" and projects the result into the "fullName" field in the
// view.
projectStage := bson.D{
{"$project", bson.D{
{"_id", 0},
{"fullName", bson.D{
{"$concat", []string{"$firstName", " ", "$lastName"}},
}},
}},
}
pipeline := mongo.Pipeline{projectStage}
// Specify the Collation option to set a default collation for the view.
opts := options.CreateView().SetCollation(&options.Collation{
Locale: "en_US",
})
if err := db.CreateView(context.TODO(), "usernames", "users", pipeline, opts); err != nil {
log.Fatal(err)
}
}
func ExampleDatabase_ListCollectionNames() {
var db *mongo.Database
// use a filter to only select capped collections
result, err := db.ListCollectionNames(context.TODO(), bson.D{{"options.capped", true}})
if err != nil {
log.Fatal(err)
}
for _, coll := range result {
fmt.Println(coll)
}
}
func ExampleDatabase_RunCommand() {
var db *mongo.Database
// run an explain command to see the query plan for when a "find" is executed on collection "bar"
// specify the ReadPreference option to explicitly set the read preference to primary
findCmd := bson.D{{"find", "bar"}}
command := bson.D{{"explain", findCmd}}
opts := options.RunCmd().SetReadPreference(readpref.Primary())
var result bson.M
if err := db.RunCommand(context.TODO(), command, opts).Decode(&result); err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
func ExampleDatabase_Watch() {
var db *mongo.Database
// specify a pipeline that will only match "insert" events
// specify the MaxAwaitTimeOption to have each attempt wait two seconds for new documents
matchStage := bson.D{{"$match", bson.D{{"operationType", "insert"}}}}
opts := options.ChangeStream().SetMaxAwaitTime(2 * time.Second)
changeStream, err := db.Watch(context.TODO(), mongo.Pipeline{matchStage}, opts)
if err != nil {
log.Fatal(err)
}
// print out all change stream events in the order they're received
// see the mongo.ChangeStream documentation for more examples of using change streams
for changeStream.Next(context.TODO()) {
fmt.Println(changeStream.Current)
}
}
// Collection examples
func ExampleCollection_Aggregate() {
var coll *mongo.Collection
// specify a pipeline that will return the number of times each name appears in the collection
// specify the MaxTime option to limit the amount of time the operation can run on the server
groupStage := bson.D{
{"$group", bson.D{
{"_id", "$name"},
{"numTimes", bson.D{
{"$sum", 1},
}},
}},
}
opts := options.Aggregate().SetMaxTime(2 * time.Second)
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{groupStage}, opts)
if err != nil {
log.Fatal(err)
}
// get a list of all returned documents and print them out
// see the mongo.Cursor documentation for more examples of using cursors
var results []bson.M
if err = cursor.All(context.TODO(), &results); err != nil {
log.Fatal(err)
}
for _, result := range results {
fmt.Printf("name %v appears %v times\n", result["_id"], result["numTimes"])
}
}
func ExampleCollection_BulkWrite() {
var coll *mongo.Collection
var firstID, secondID primitive.ObjectID
// update the "email" field for two users
// for each update, specify the Upsert option to insert a new document if a document matching the filter isn't
// found
// set the Ordered option to false to allow both operations to happen even if one of them errors
firstUpdate := bson.D{{"$set", bson.D{{"email", "firstEmail@example.com"}}}}
secondUpdate := bson.D{{"$set", bson.D{{"email", "secondEmail@example.com"}}}}
models := []mongo.WriteModel{
mongo.NewUpdateOneModel().SetFilter(bson.D{{"_id", firstID}}).SetUpdate(firstUpdate).SetUpsert(true),
mongo.NewUpdateOneModel().SetFilter(bson.D{{"_id", secondID}}).SetUpdate(secondUpdate).SetUpsert(true),
}
opts := options.BulkWrite().SetOrdered(false)
res, err := coll.BulkWrite(context.TODO(), models, opts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("inserted %v and deleted %v documents\n", res.InsertedCount, res.DeletedCount)
}
func ExampleCollection_CountDocuments() {
var coll *mongo.Collection
// count the number of times the name "Bob" appears in the collection
// specify the MaxTime option to limit the amount of time the operation can run on the server
opts := options.Count().SetMaxTime(2 * time.Second)
count, err := coll.CountDocuments(context.TODO(), bson.D{{"name", "Bob"}}, opts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("name Bob appears in %v documents", count)
}
func ExampleCollection_DeleteMany() {
var coll *mongo.Collection
// delete all documents in which the "name" field is "Bob" or "bob"
// specify the Collation option to provide a collation that will ignore case for string comparisons
opts := options.Delete().SetCollation(&options.Collation{
Locale: "en_US",
Strength: 1,
CaseLevel: false,
})
res, err := coll.DeleteMany(context.TODO(), bson.D{{"name", "bob"}}, opts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("deleted %v documents\n", res.DeletedCount)
}
func ExampleCollection_DeleteOne() {
var coll *mongo.Collection
// delete at most one document in which the "name" field is "Bob" or "bob"
// specify the SetCollation option to provide a collation that will ignore case for string comparisons
opts := options.Delete().SetCollation(&options.Collation{
Locale: "en_US",
Strength: 1,
CaseLevel: false,
})
res, err := coll.DeleteOne(context.TODO(), bson.D{{"name", "bob"}}, opts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("deleted %v documents\n", res.DeletedCount)
}
func ExampleCollection_Distinct() {
var coll *mongo.Collection
// find all unique values for the "name" field for documents in which the "age" field is greater than 25
// specify the MaxTime option to limit the amount of time the operation can run on the server
filter := bson.D{{"age", bson.D{{"$gt", 25}}}}
opts := options.Distinct().SetMaxTime(2 * time.Second)
values, err := coll.Distinct(context.TODO(), "name", filter, opts)
if err != nil {
log.Fatal(err)
}
for _, value := range values {
fmt.Println(value)
}
}
func ExampleCollection_EstimatedDocumentCount() {
var coll *mongo.Collection
// get and print an estimated of the number of documents in the collection
// specify the MaxTime option to limit the amount of time the operation can run on the server
opts := options.EstimatedDocumentCount().SetMaxTime(2 * time.Second)
count, err := coll.EstimatedDocumentCount(context.TODO(), opts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("estimated document count: %v", count)
}
func ExampleCollection_Find() {
var coll *mongo.Collection
// find all documents in which the "name" field is "Bob"
// specify the Sort option to sort the returned documents by age in ascending order
opts := options.Find().SetSort(bson.D{{"age", 1}})
cursor, err := coll.Find(context.TODO(), bson.D{{"name", "Bob"}}, opts)
if err != nil {
log.Fatal(err)
}
// get a list of all returned documents and print them out
// see the mongo.Cursor documentation for more examples of using cursors
var results []bson.M
if err = cursor.All(context.TODO(), &results); err != nil {
log.Fatal(err)
}
for _, result := range results {
fmt.Println(result)
}
}
func ExampleCollection_FindOne() {
var coll *mongo.Collection
var id primitive.ObjectID
// find the document for which the _id field matches id
// specify the Sort option to sort the documents by age
// the first document in the sorted order will be returned
opts := options.FindOne().SetSort(bson.D{{"age", 1}})
var result bson.M
err := coll.FindOne(context.TODO(), bson.D{{"_id", id}}, opts).Decode(&result)
if err != nil {
// ErrNoDocuments means that the filter did not match any documents in the collection
if err == mongo.ErrNoDocuments {
return
}
log.Fatal(err)
}
fmt.Printf("found document %v", result)
}
func ExampleCollection_FindOneAndDelete() {
var coll *mongo.Collection
var id primitive.ObjectID
// find and delete the document for which the _id field matches id
// specify the Projection option to only include the name and age fields in the returned document
opts := options.FindOneAndDelete().SetProjection(bson.D{{"name", 1}, {"age", 1}})
var deletedDocument bson.M
err := coll.FindOneAndDelete(context.TODO(), bson.D{{"_id", id}}, opts).Decode(&deletedDocument)
if err != nil {
// ErrNoDocuments means that the filter did not match any documents in the collection
if err == mongo.ErrNoDocuments {
return
}
log.Fatal(err)
}
fmt.Printf("deleted document %v", deletedDocument)
}
func ExampleCollection_FindOneAndReplace() {
var coll *mongo.Collection
var id primitive.ObjectID
// find the document for which the _id field matches id and add a field called "location"
// specify the Upsert option to insert a new document if a document matching the filter isn't found
opts := options.FindOneAndReplace().SetUpsert(true)
filter := bson.D{{"_id", id}}
replacement := bson.D{{"location", "NYC"}}
var replacedDocument bson.M
err := coll.FindOneAndReplace(context.TODO(), filter, replacement, opts).Decode(&replacedDocument)
if err != nil {
// ErrNoDocuments means that the filter did not match any documents in the collection
if err == mongo.ErrNoDocuments {
return
}
log.Fatal(err)
}
fmt.Printf("replaced document %v", replacedDocument)
}
func ExampleCollection_FindOneAndUpdate() {
var coll *mongo.Collection
var id primitive.ObjectID
// find the document for which the _id field matches id and set the email to "newemail@example.com"
// specify the Upsert option to insert a new document if a document matching the filter isn't found
opts := options.FindOneAndUpdate().SetUpsert(true)
filter := bson.D{{"_id", id}}
update := bson.D{{"$set", bson.D{{"email", "newemail@example.com"}}}}
var updatedDocument bson.M
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDocument)
if err != nil {
// ErrNoDocuments means that the filter did not match any documents in the collection
if err == mongo.ErrNoDocuments {
return
}
log.Fatal(err)
}
fmt.Printf("updated document %v", updatedDocument)
}
func ExampleCollection_InsertMany() {
var coll *mongo.Collection
// insert documents {name: "Alice"} and {name: "Bob"}
// set the Ordered option to false to allow both operations to happen even if one of them errors
docs := []interface{}{
bson.D{{"name", "Alice"}},
bson.D{{"name", "Bob"}},
}
opts := options.InsertMany().SetOrdered(false)
res, err := coll.InsertMany(context.TODO(), docs, opts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("inserted documents with IDs %v\n", res.InsertedIDs)
}
func ExampleCollection_InsertOne() {
var coll *mongo.Collection
// insert the document {name: "Alice"}
res, err := coll.InsertOne(context.TODO(), bson.D{{"name", "Alice"}})
if err != nil {
log.Fatal(err)
}
fmt.Printf("inserted document with ID %v\n", res.InsertedID)
}
func ExampleCollection_ReplaceOne() {
var coll *mongo.Collection
var id primitive.ObjectID
// find the document for which the _id field matches id and add a field called "location"
// specify the Upsert option to insert a new document if a document matching the filter isn't found
opts := options.Replace().SetUpsert(true)
filter := bson.D{{"_id", id}}
replacement := bson.D{{"location", "NYC"}}
result, err := coll.ReplaceOne(context.TODO(), filter, replacement, opts)
if err != nil {
log.Fatal(err)
}
if result.MatchedCount != 0 {
fmt.Println("matched and replaced an existing document")
return
}
if result.UpsertedCount != 0 {
fmt.Printf("inserted a new document with ID %v\n", result.UpsertedID)
}
}
func ExampleCollection_UpdateMany() {
var coll *mongo.Collection
// increment the age for all users whose birthday is today
today := time.Now().Format("01-01-1970")
filter := bson.D{{"birthday", today}}
update := bson.D{{"$inc", bson.D{{"age", 1}}}}
result, err := coll.UpdateMany(context.TODO(), filter, update)
if err != nil {
log.Fatal(err)
}
if result.MatchedCount != 0 {
fmt.Println("matched and replaced an existing document")
return
}
}
func ExampleCollection_UpdateOne() {
var coll *mongo.Collection
var id primitive.ObjectID
// find the document for which the _id field matches id and set the email to "newemail@example.com"
// specify the Upsert option to insert a new document if a document matching the filter isn't found
opts := options.Update().SetUpsert(true)
filter := bson.D{{"_id", id}}
update := bson.D{{"$set", bson.D{{"email", "newemail@example.com"}}}}
result, err := coll.UpdateOne(context.TODO(), filter, update, opts)
if err != nil {
log.Fatal(err)
}
if result.MatchedCount != 0 {
fmt.Println("matched and replaced an existing document")
return
}
if result.UpsertedCount != 0 {
fmt.Printf("inserted a new document with ID %v\n", result.UpsertedID)
}
}
func ExampleCollection_Watch() {
var collection *mongo.Collection
// specify a pipeline that will only match "insert" events
// specify the MaxAwaitTimeOption to have each attempt wait two seconds for new documents
matchStage := bson.D{{"$match", bson.D{{"operationType", "insert"}}}}
opts := options.ChangeStream().SetMaxAwaitTime(2 * time.Second)
changeStream, err := collection.Watch(context.TODO(), mongo.Pipeline{matchStage}, opts)
if err != nil {
log.Fatal(err)
}
// print out all change stream events in the order they're received
// see the mongo.ChangeStream documentation for more examples of using change streams
for changeStream.Next(context.TODO()) {
fmt.Println(changeStream.Current)
}
}
// Session examples
func ExampleWithSession() {
var client *mongo.Client // assume client is configured with write concern majority and read preference primary
// Specify the DefaultReadConcern option so any transactions started through the session will have read concern
// majority.
// The DefaultReadPreference and DefaultWriteConcern options aren't specified so they will be inheritied from client
// and be set to primary and majority, respectively.
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
sess, err := client.StartSession(opts)
if err != nil {
log.Fatal(err)
}
defer sess.EndSession(context.TODO())
// Call WithSession to start a transaction within the new session.
err = mongo.WithSession(context.TODO(), sess, func(sessCtx mongo.SessionContext) error {
// Use sessCtx as the Context parameter for InsertOne and FindOne so both operations are run under the new
// Session.
if err := sess.StartTransaction(); err != nil {
return err
}
coll := client.Database("db").Collection("coll")
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
if err != nil {
// Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
// successfully even if the context passed to mongo.WithSession is changed to have a timeout.
_ = sess.AbortTransaction(context.Background())
return err
}
var result bson.M
if err = coll.FindOne(sessCtx, bson.D{{"_id", res.InsertedID}}).Decode(result); err != nil {
// Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
// successfully even if the context passed to mongo.WithSession is changed to have a timeout.
_ = sess.AbortTransaction(context.Background())
return err
}
fmt.Println(result)
// Use context.Background() to ensure that the commit can complete successfully even if the context passed to
// mongo.WithSession is changed to have a timeout.
return sess.CommitTransaction(context.Background())
})
}
func ExampleClient_UseSessionWithOptions() {
var client *mongo.Client
// Specify the DefaultReadConcern option so any transactions started through the session will have read concern
// majority.
// The DefaultReadPreference and DefaultWriteConcern options aren't specified so they will be inheritied from client
// and be set to primary and majority, respectively.
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
err := client.UseSessionWithOptions(context.TODO(), opts, func(sessCtx mongo.SessionContext) error {
// Use sessCtx as the Context parameter for InsertOne and FindOne so both operations are run under the new
// Session.
if err := sessCtx.StartTransaction(); err != nil {
return err
}
coll := client.Database("db").Collection("coll")
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
if err != nil {
// Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
// successfully even if the context passed to mongo.WithSession is changed to have a timeout.
_ = sessCtx.AbortTransaction(context.Background())
return err
}
var result bson.M
if err = coll.FindOne(sessCtx, bson.D{{"_id", res.InsertedID}}).Decode(result); err != nil {
// Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
// successfully even if the context passed to mongo.WithSession is changed to have a timeout.
_ = sessCtx.AbortTransaction(context.Background())
return err
}
fmt.Println(result)
// Use context.Background() to ensure that the commit can complete successfully even if the context passed to
// mongo.WithSession is changed to have a timeout.
return sessCtx.CommitTransaction(context.Background())
})
if err != nil {
log.Fatal(err)
}
}
func ExampleClient_StartSession_withTransaction() {
var client *mongo.Client // assume client is configured with write concern majority and read preference primary
// Specify the DefaultReadConcern option so any transactions started through the session will have read concern
// majority.
// The DefaultReadPreference and DefaultWriteConcern options aren't specified so they will be inheritied from client
// and be set to primary and majority, respectively.
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
sess, err := client.StartSession(opts)
if err != nil {
log.Fatal(err)
}
defer sess.EndSession(context.TODO())
// Specify the ReadPreference option to set the read preference to primary preferred for this transaction.
txnOpts := options.Transaction().SetReadPreference(readpref.PrimaryPreferred())
result, err := sess.WithTransaction(context.TODO(), func(sessCtx mongo.SessionContext) (interface{}, error) {
// Use sessCtx as the Context parameter for InsertOne and FindOne so both operations are run in a
// transaction.
coll := client.Database("db").Collection("coll")
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
if err != nil {
return nil, err
}
var result bson.M
if err = coll.FindOne(sessCtx, bson.D{{"_id", res.InsertedID}}).Decode(result); err != nil {
return nil, err
}
return result, err
}, txnOpts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("result: %v\n", result)
}
func ExampleNewSessionContext() {
var client *mongo.Client
// Create a new Session and SessionContext.
sess, err := client.StartSession()
if err != nil {
panic(err)
}
defer sess.EndSession(context.TODO())
sessCtx := mongo.NewSessionContext(context.TODO(), sess)
// Start a transaction and sessCtx as the Context parameter to InsertOne and FindOne so both operations will be
// run in the transaction.
if err = sess.StartTransaction(); err != nil {
panic(err)
}
coll := client.Database("db").Collection("coll")
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
if err != nil {
// Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
// successfully even if the context passed to NewSessionContext is changed to have a timeout.
_ = sess.AbortTransaction(context.Background())
panic(err)
}
var result bson.M
if err = coll.FindOne(sessCtx, bson.D{{"_id", res.InsertedID}}).Decode(&result); err != nil {
// Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
// successfully even if the context passed to NewSessionContext is changed to have a timeout.
_ = sess.AbortTransaction(context.Background())
panic(err)
}
fmt.Printf("result: %v\n", result)
// Commit the transaction so the inserted document will be stored. Use context.Background() to ensure that the
// commit can complete successfully even if the context passed to NewSessionContext is changed to have a timeout.
if err = sess.CommitTransaction(context.Background()); err != nil {
panic(err)
}
}
// Cursor examples
func ExampleCursor_All() {
var cursor *mongo.Cursor
var results []bson.M
if err := cursor.All(context.TODO(), &results); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleCursor_Next() {
var cursor *mongo.Cursor
defer cursor.Close(context.TODO())
// Iterate the cursor and print out each document until the cursor is exhausted or there is an error getting the
// next document.
for cursor.Next(context.TODO()) {
// A new result variable should be declared for each document.
var result bson.M
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
if err := cursor.Err(); err != nil {
log.Fatal(err)
}
}
func ExampleCursor_TryNext() {
var cursor *mongo.Cursor
defer cursor.Close(context.TODO())
// Iterate the cursor and print out each document until the cursor is exhausted or there is an error getting the
// next document.
for {
if cursor.TryNext(context.TODO()) {
// A new result variable should be declared for each document.
var result bson.M
if err := cursor.Decode(&result); err != nil {
log.Fatal(err)
}
fmt.Println(result)
continue
}
// If TryNext returns false, the next document is not yet available, the cursor was exhausted and was closed, or
// an error occured. TryNext should only be called again for the empty batch case.
if err := cursor.Err(); err != nil {
log.Fatal(err)
}
if cursor.ID() == 0 {
break
}
}
}
func ExampleCursor_RemainingBatchLength() {
// Because we're using a tailable cursor, this must be a handle to a capped collection.
var coll *mongo.Collection
// Create a tailable await cursor. Specify the MaxAwaitTime option so requests to get more data will return if there
// are no documents available after two seconds.
findOpts := options.Find().
SetCursorType(options.TailableAwait).
SetMaxAwaitTime(2 * time.Second)
cursor, err := coll.Find(context.TODO(), bson.D{}, findOpts)
if err != nil {
panic(err)
}
for {
// Iterate the cursor using TryNext.
if cursor.TryNext(context.TODO()) {
fmt.Println(cursor.Current)
}
// Handle cursor errors or the cursor being closed by the server.
if err = cursor.Err(); err != nil {
panic(err)
}
if cursor.ID() == 0 {
panic("cursor was unexpectedly closed by the server")
}
// Use the RemainingBatchLength function to rate-limit the number of network requests the driver does. If the
// current batch is empty, sleep for a short amount of time to let documents build up on the server before
// the next TryNext call, which will do a network request.
if cursor.RemainingBatchLength() == 0 {
time.Sleep(100 * time.Millisecond)
}
}
}
// ChangeStream examples
func ExampleChangeStream_Next() {
var stream *mongo.ChangeStream
defer stream.Close(context.TODO())
// Iterate the change stream and print out each event.
// Because the Next call blocks until an event is available, another way to iterate the change stream is to call
// Next in a goroutine and pass in a context that can be cancelled to abort the call.
for stream.Next(context.TODO()) {
// A new event variable should be declared for each event.
var event bson.M
if err := stream.Decode(&event); err != nil {
log.Fatal(err)
}
fmt.Println(event)
}
if err := stream.Err(); err != nil {
log.Fatal(err)
}
}
func ExampleChangeStream_TryNext() {
var stream *mongo.ChangeStream
defer stream.Close(context.TODO())
// Iterate the change stream and print out each event until the change stream is closed by the server or there is an
// error getting the next event.
for {
if stream.TryNext(context.TODO()) {
// A new event variable should be declared for each event.
var event bson.M
if err := stream.Decode(&event); err != nil {
log.Fatal(err)
}
fmt.Println(event)
continue
}
// If TryNext returns false, the next change is not yet available, the change stream was closed by the server,
// or an error occurred. TryNext should only be called again for the empty batch case.
if err := stream.Err(); err != nil {
log.Fatal(err)
}
if stream.ID() == 0 {
break
}
}
}
func ExampleChangeStream_ResumeToken() {
var client *mongo.Client
var stream *mongo.ChangeStream // assume stream was created via client.Watch()
cancelCtx, cancel := context.WithCancel(context.TODO())
defer cancel()
var wg sync.WaitGroup
wg.Add(1)
// Run a goroutine to process events.
go func() {
for stream.Next(cancelCtx) {
fmt.Println(stream.Current)
}
wg.Done()
}()
// Assume client needs to be disconnected. Cancel the context being used by the goroutine to abort any
// in-progres Next calls and wait for the goroutine to exit.
cancel()
wg.Wait()
// Before disconnecting the client, store the last seen resume token for the change stream.
resumeToken := stream.ResumeToken()
_ = client.Disconnect(context.TODO())
// Once a new client is created, the change stream can be re-created. Specify resumeToken as the ResumeAfter option
// so only events that occurred after resumeToken will be returned.
var newClient *mongo.Client
opts := options.ChangeStream().SetResumeAfter(resumeToken)
newStream, err := newClient.Watch(context.TODO(), mongo.Pipeline{}, opts)
if err != nil {
log.Fatal(err)
}
defer newStream.Close(context.TODO())
}
// IndexView examples
func ExampleIndexView_CreateMany() {
var indexView *mongo.IndexView
// Create two indexes: {name: 1, email: 1} and {name: 1, age: 1}
// For the first index, specify no options. The name will be generated as "name_1_email_1" by the driver.
// For the second index, specify the Name option to explicitly set the name to "nameAge".
models := []mongo.IndexModel{
{
Keys: bson.D{{"name", 1}, {"email", 1}},
},
{
Keys: bson.D{{"name", 1}, {"age", 1}},
Options: options.Index().SetName("nameAge"),
},
}
// Specify the MaxTime option to limit the amount of time the operation can run on the server
opts := options.CreateIndexes().SetMaxTime(2 * time.Second)
names, err := indexView.CreateMany(context.TODO(), models, opts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("created indexes %v\n", names)
}
func ExampleIndexView_List() {
var indexView *mongo.IndexView
// Specify the MaxTime option to limit the amount of time the operation can run on the server
opts := options.ListIndexes().SetMaxTime(2 * time.Second)
cursor, err := indexView.List(context.TODO(), opts)
if err != nil {
log.Fatal(err)
}
// Get a slice of all indexes returned and print them out.
var results []bson.M
if err = cursor.All(context.TODO(), &results); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}