/
template.go
1549 lines (1472 loc) 路 64.9 KB
/
template.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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mongo
import (
"context"
"errors"
"github.com/GabrielHCataldo/go-helper/helper"
"github.com/GabrielHCataldo/go-logger/logger"
"github.com/GabrielHCataldo/go-mongo-template/internal/util"
"github.com/GabrielHCataldo/go-mongo-template/mongo/option"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"reflect"
"strings"
)
// Pipeline is a type that makes creating aggregation pipelines easier. It is a
// helper and is intended for serializing to BSON.
//
// Example usage:
//
// Pipeline{
// {{"$group", bson.D{{"_id", "$state"}, {"totalPop", bson.D{{"$sum", "$pop"}}}}}},
// {{"$match", bson.D{{"totalPop", bson.D{{"$gte", 10*1000*1000}}}}}},
// }
type Pipeline []bson.D
// DeleteResult is the result type returned by DeleteOne and DeleteMany operations.
type DeleteResult struct {
// The number of documents deleted.
DeletedCount int64
}
// UpdateResult is the result type returned from UpdateOne, UpdateMany, and ReplaceOne operations.
type UpdateResult struct {
// The number of documents matched by the filter.
MatchedCount int64
// The number of documents modified by the operation.
ModifiedCount int64
// The number of documents upserted by the operation.
UpsertedCount int64
// The _id field of the upserted document, or nil if no upsert was done.
UpsertedID any
}
// IndexSpecification represents an index in a database. This type is returned by the IndexView.ListSpecifications
// function and is also used in the CollectionSpecification type.
type IndexSpecification struct {
// The index name.
Name string
// The namespace for the index. This is a string in the format "databaseName.collectionName".
Namespace string
// The keys specification document for the index.
KeysDocument bson.Raw
// The index version.
Version int32
// The length of time, in seconds, for documents to remain in the collection. The default value is 0, which means
// that documents will remain in the collection until they're explicitly deleted or the collection is dropped.
ExpireAfterSeconds *int32
// If true, the index will only reference documents that contain the fields specified in the index. The default is
// false.
Sparse *bool
// If true, the collection will not accept insertion or update of documents where the index key value matches an
// existing value in the index. The default is false.
Unique *bool
// The clustered index.
Clustered *bool
}
type Template struct {
client *mongo.Client
session mongo.Session
}
var globalOption = &option.Global{}
func NewTemplate(ctx context.Context, opts ...*options.ClientOptions) (*Template, error) {
conn, err := mongo.Connect(ctx, opts...)
if helper.IsNotNil(err) {
return nil, err
}
err = conn.Ping(ctx, nil)
if helper.IsNotNil(err) {
return nil, err
}
return &Template{
client: conn,
}, nil
}
// SetGlobalOption sets value for the mongo global options.
func (t *Template) SetGlobalOption(opt *option.Global) {
if helper.IsNotNil(opt) {
globalOption = opt
} else {
globalOption = &option.Global{}
}
}
// InsertOne executes an insert command to insert a single document into the collection.
//
// The document parameter must be a structure pointer to be inserted, it must be non-zero. If it does not have the _id
// field when transformed into BSON, the field value is automatically generated and will be added to the document
// pointer provided.
//
// The opts parameter can be used to specify options for the operation (see the option.Change documentation.)
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/insert/.
func (t *Template) InsertOne(ctx context.Context, document any, opts ...*option.InsertOne) error {
opt := option.MergeInsertOneByParams(opts, globalOption)
err := t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
err = t.insertOne(sc, document, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return err
}
// InsertMany executes an insert command to insert multiple documents into the collection. If recording errors occur
// during operation you can configure automatic rollback, see the option.insertMany documentation.
//
// The documents parameter must be a structure pointer slice to be inserted. The slice cannot be null or empty.
// All elements must be non-zero. For any document that does not have the _id field when transformed into BSON,
// the field value will be automatically generated and added to the slice pointer.
//
// The opts parameter can be used to specify options for the operation (see the option.Change documentation.)
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/insert/.
func (t *Template) InsertMany(ctx context.Context, documents any, opts ...*option.InsertMany) error {
opt := option.MergeInsertManyByParams(opts, globalOption)
err := t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
err = t.insertMany(sc, documents, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return err
}
// DeleteOne executes a delete command to delete at most one document from the collection.
//
// The filter parameter must be a document containing query operators and can be used to select the document to be
// deleted. It cannot be null. If the filter does not match any documents, the operation succeeds and a DeleteResult
// with a DeletedCount of 0 will be returned. If the filter matches multiple documents, one will be selected from the list
// matching set.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.Delete documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/delete/.
func (t *Template) DeleteOne(ctx context.Context, filter, ref any, opts ...*option.Delete) (*DeleteResult, error) {
var result *DeleteResult
var err error
opt := option.MergeDeleteByParams(opts, globalOption)
err = t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
result, err = t.deleteOne(sc, filter, ref, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return result, err
}
// DeleteOneById executes an update command to update the document whose _id value matches the provided ID in the collection.
// This is equivalent to running DeleteOne(ctx, bson.D{{"_id", id}}, ref, opts...).
//
// The id parameter is the _id of the document to be updated. It cannot be nil. If the ID does not match any documents,
// the operation will succeed and an UpdateResult with a MatchedCount of 0 will be returned.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.Delete documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/delete/.
func (t *Template) DeleteOneById(ctx context.Context, id, ref any, opts ...*option.Delete) (*DeleteResult, error) {
var result *DeleteResult
var err error
opt := option.MergeDeleteByParams(opts, globalOption)
err = t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
result, err = t.deleteOne(sc, bson.D{{"_id", id}}, ref, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return result, err
}
// DeleteMany executes a delete command to delete documents from the collection.
//
// The filter parameter must be a document containing query operators and can be used to select the documents to
// be deleted. It cannot be nil. An empty document (e.g. bson.D{}) should be used to delete all documents in the
// collection. If the filter does not match any documents, the operation will succeed and a DeleteResult with a
// DeletedCount of 0 will be returned.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.Delete documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/delete/.
func (t *Template) DeleteMany(ctx context.Context, filter, ref any, opts ...*option.Delete) (*DeleteResult, error) {
var result *DeleteResult
var err error
opt := option.MergeDeleteByParams(opts, globalOption)
err = t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
result, err = t.deleteMany(sc, filter, ref, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return result, err
}
// UpdateOneById executes an update command to update the document whose _id value matches the provided ID in the collection.
// This is equivalent to running UpdateOne(ctx, bson.D{{"_id", id}}, update, ref, opts...).
//
// The id parameter is the _id of the document to be updated. It cannot be nil. If the ID does not match any documents,
// the operation will succeed and an UpdateResult with a MatchedCount of 0 will be returned.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The update parameter must be a document containing update operators
// (https://www.mongodb.com/docs/manual/reference/operator/update/) and can be used to specify the modifications to be
// made to the selected document. It cannot be nil or empty.
//
// The opts parameter can be used to specify options for the operation (see the option.Update documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/update/.
func (t *Template) UpdateOneById(ctx context.Context, id, update, ref any, opts ...*option.Update) (*UpdateResult, error) {
var result *UpdateResult
var err error
opt := option.MergeUpdateByParams(opts, globalOption)
err = t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
result, err = t.updateOne(sc, bson.D{{"_id", id}}, update, ref, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return result, err
}
// UpdateOne executes an update command to update at most one document in the collection.
//
// The filter parameter must be a document containing query operators and can be used to select the document to be
// updated. It cannot be nil. If the filter does not match any documents, the operation will succeed and an UpdateResult
// with a MatchedCount of 0 will be returned. If the filter matches multiple documents, one will be selected from the
// matched set and MatchedCount will equal 1.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The update parameter must be a document containing update operators
// (https://www.mongodb.com/docs/manual/reference/operator/update/) and can be used to specify the modifications to be
// made to the selected document. It cannot be nil or empty.
//
// The opts parameter can be used to specify options for the operation (see the option.Update documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/update/.
func (t *Template) UpdateOne(ctx context.Context, filter any, update, ref any, opts ...*option.Update) (*UpdateResult,
error) {
var result *UpdateResult
var err error
opt := option.MergeUpdateByParams(opts, globalOption)
err = t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
result, err = t.updateOne(sc, filter, update, ref, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return result, err
}
// UpdateMany executes an update command to update documents in the collection.
//
// The filter parameter must be a document containing query operators and can be used to select the documents to be
// updated. It cannot be nil. If the filter does not match any documents, the operation will succeed and an UpdateResult
// with a MatchedCount of 0 will be returned.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The update parameter must be a document containing update operators
// (https://www.mongodb.com/docs/manual/reference/operator/update/) and can be used to specify the modifications to be made
// to the selected documents. It cannot be nil or empty.
//
// The opts parameter can be used to specify options for the operation (see the option.Update documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/update/.
func (t *Template) UpdateMany(ctx context.Context, filter any, update, ref any, opts ...*option.Update) (*UpdateResult,
error) {
var result *UpdateResult
var err error
opt := option.MergeUpdateByParams(opts, globalOption)
err = t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
result, err = t.updateMany(sc, filter, update, ref, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return result, err
}
// ReplaceOne executes an update command to replace at most one document in the collection.
//
// The filter parameter must be a document containing query operators and can be used to select the document to be
// replaced. It cannot be nil. If the filter does not match any documents, the operation will succeed and an
// UpdateResult with a MatchedCount of 0 will be returned. If the filter matches multiple documents, one will be
// selected from the matched set and MatchedCount will equal 1.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The replacement parameter must be a document that will be used to replace the selected document. It cannot be nil
// and cannot contain any update operators (https://www.mongodb.com/docs/manual/reference/operator/update/).
//
// The opts parameter can be used to specify options for the operation (see the option.Replace documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/update/.
func (t *Template) ReplaceOne(ctx context.Context, filter any, update, ref any, opts ...*option.Replace) (*UpdateResult,
error) {
var result *UpdateResult
var err error
opt := option.MergeReplaceByParams(opts, globalOption)
err = t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
result, err = t.replaceOne(sc, filter, update, ref, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return result, err
}
// ReplaceOneById executes an update command to update the document whose _id value matches the provided ID in the collection.
// This is equivalent to running ReplaceOne(ctx, bson.D{{"_id", id}}, replacement, ref, opts...).
//
// The id parameter is the _id of the document to be updated. It cannot be nil. If the ID does not match any documents,
// the operation will succeed and an UpdateResult with a MatchedCount of 0 will be returned.
//
// The opts parameter can be used to specify options for the operation (see the option.Replace documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/update/.
func (t *Template) ReplaceOneById(ctx context.Context, id, replacement, ref any, opts ...*option.Replace) (*UpdateResult,
error) {
var result *UpdateResult
var err error
opt := option.MergeReplaceByParams(opts, globalOption)
err = t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
result, err = t.replaceOne(sc, bson.D{{"_id", id}}, replacement, ref, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return result, err
}
// FindOneById executes a search command whose _id value matches the ID given in the collection.
// This is equivalent to running FindOne(ctx, bson.D{{"_id", id}}, dest, opts...).
//
// The id parameter must be a document containing query operators and can be used to select the document to be
// returned. It cannot be null. If the id does not match any document, the error ErrNoDocuments will be returned.
//
// The dest parameter must be a pointer to the return expected by the operation, it is important to have the
// database and collection tags configured.
//
// The opts parameter can be used to specify options for this operation (see the option.FindOneById documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.
func (t *Template) FindOneById(ctx context.Context, id, dest any, opts ...*option.FindOneById) error {
opt := option.MergeFindOneByIdByParams(opts)
return t.findOne(ctx, bson.D{{"_id", id}}, dest, &option.FindOne{
AllowPartialResults: opt.AllowPartialResults,
Collation: opt.Collation,
Comment: opt.Comment,
Hint: opt.Hint,
Max: opt.Max,
MaxTime: opt.MaxTime,
Min: opt.Min,
Projection: opt.Projection,
ReturnKey: opt.ReturnKey,
ShowRecordID: opt.ShowRecordID,
})
}
// FindOne executes a find command, if successful it returns the corresponding documents in the collection in the dest
// parameter with return error nil. Otherwise, it returns corresponding error.
//
// The id parameter must be a document containing query operators and can be used to select the document to be
// returned. It cannot be null. If the id does not match any document, the error ErrNoDocuments will be returned.
//
// The dest parameter must be a pointer to the return expected by the operation, it is important to have the
// database and collection tags configured.
//
// The opts parameter can be used to specify options for this operation (see the option.FindOne documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.
func (t *Template) FindOne(ctx context.Context, filter, dest any, opts ...*option.FindOne) error {
return t.findOne(ctx, filter, dest, opts...)
}
// FindOneAndDeleteById executes a findAndModify command whose _id value matches the ID given in the collection.
// This is equivalent to running FindOneAndDelete(ctx, bson.D{{"_id", id}}, dest, opts...)
//
// The id parameter is the _id of the document to be replaced. It cannot be null. If the filter does not match any
// documents, an error set to ErrNoDocuments will be returned. If the filter matches multiple documents,
// one will be selected from the matching set.
//
// The dest parameter must be a pointer to the return expected by the operation, it is important to have the
// database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.FindOneAndDelete documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.
func (t *Template) FindOneAndDeleteById(ctx context.Context, id, dest any, opts ...*option.FindOneAndDelete) error {
opt := option.MergeFindOneAndDeleteByParams(opts, globalOption)
err := t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
err = t.findOneAndDelete(sc, bson.D{{"_id", id}}, dest, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return err
}
// FindOneAndDelete executes a findAndModify command to delete at most one document from the collection. and returns the
// document as it appeared before deletion in the dest parameter.
//
// The filter parameter must be a document containing query operators and can be used to select the document to be
// deleted. It cannot be null. If the filter does not match any documents, an error set to ErrNoDocuments will be
// returned. If the filter matches multiple documents, one will be selected from the matching set.
//
// The dest parameter must be a pointer to the return expected by the operation, it is important to have the
// database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.FindOneAndDelete documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.
func (t *Template) FindOneAndDelete(ctx context.Context, filter, dest any, opts ...*option.FindOneAndDelete) error {
opt := option.MergeFindOneAndDeleteByParams(opts, globalOption)
err := t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
err = t.findOneAndDelete(sc, filter, dest, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return err
}
// FindOneAndReplaceById executes a findAndModify command whose _id value matches the ID given in the collection.
// This is equivalent to running FindOneAndReplace(ctx, bson.D{{"_id", id}}, dest, opts...).
//
// The id parameter is the _id of the document to be replaced. It cannot be null. If the filter does not match any
// documents, an error set to ErrNoDocuments will be returned. If the filter matches multiple documents,
// one will be selected from the matching set.
//
// The replacement parameter must be a document that will be used to replace the selected document. It cannot be nil
// and cannot contain any update operators (https://www.mongodb.com/docs/manual/reference/operator/update/).
//
// The dest parameter must be a pointer to the return expected by the operation, it is important to have the
// database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.FindOneAndReplace documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.
func (t *Template) FindOneAndReplaceById(ctx context.Context, id, replacement, dest any, opts ...*option.FindOneAndReplace) error {
opt := option.MergeFindOneAndReplaceByParams(opts, globalOption)
err := t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
err = t.findOneAndReplace(sc, bson.D{{"_id", id}}, replacement, dest, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return err
}
// FindOneAndReplace executes a findAndModify command to replace at most one document in the collection
// and returns the document as it appeared before replacement in the dest parameter.
//
// The filter parameter must be a document containing query operators and can be used to select the document to be
// replaced. It cannot be null. If the filter does not match any documents, an error set to ErrNoDocuments will be
// returned. If the filter matches multiple documents, one will be selected from the matching set.
//
// The replacement parameter must be a document that will be used to replace the selected document. It cannot be nil
// and cannot contain any update operators (https://www.mongodb.com/docs/manual/reference/operator/update/).
//
// The dest parameter must be a pointer to the return expected by the operation, it is important to have the
// database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.FindOneAndReplace documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.
func (t *Template) FindOneAndReplace(ctx context.Context, filter, replacement, dest any, opts ...*option.FindOneAndReplace) error {
opt := option.MergeFindOneAndReplaceByParams(opts, globalOption)
err := t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
err = t.findOneAndReplace(sc, filter, replacement, dest, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return err
}
// FindOneAndUpdateById executes a findAndModify command whose _id value matches the ID given in the collection.
// This is equivalent to running FindOneAndUpdate(ctx, bson.D{{"_id", id}}, dest, opts...).
//
// The id parameter is the _id of the document to be updated. It cannot be null. If the filter does not match any
// documents, an error set to ErrNoDocuments will be returned. If the filter matches multiple documents,
// one will be selected from the matching set.
//
// The update parameter must be a document containing update operators
// (https://www.mongodb.com/docs/manual/reference/operator/update/) and can be used to specify the modifications to be made
// to the selected document. It cannot be nil or empty.
//
// The opts parameter can be used to specify options for the operation (see the options.FindOneAndUpdateOptions
// documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.
func (t *Template) FindOneAndUpdateById(ctx context.Context, id, update, dest any, opts ...*option.FindOneAndUpdate) error {
opt := option.MergeFindOneAndUpdateByParams(opts, globalOption)
err := t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
err = t.findOneAndUpdate(sc, bson.D{{"_id", id}}, update, dest, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return err
}
// FindOneAndUpdate executes a findAndModify command to update at most one document in the collection and returns the
// document as it appeared before updating.
//
// The filter parameter must be a document containing query operators and can be used to select the document to be
// updated. It cannot be null. If the filter does not match any documents, an error set to ErrNoDocuments will be
// returned. If the filter matches multiple documents, one will be selected from the matching set.
//
// The update parameter must be a document containing update operators
// (https://www.mongodb.com/docs/manual/reference/operator/update/) and can be used to specify the modifications to be made
// to the selected document. It cannot be nil or empty.
//
// The opts parameter can be used to specify options for the operation (see the options.FindOneAndUpdateOptions
// documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.
func (t *Template) FindOneAndUpdate(ctx context.Context, filter, update, dest any, opts ...*option.FindOneAndUpdate) error {
opt := option.MergeFindOneAndUpdateByParams(opts, globalOption)
err := t.startSession(ctx, *opt.ForceRecreateSession)
if helper.IsNil(err) {
err = mongo.WithSession(ctx, t.session, func(sc mongo.SessionContext) error {
err = t.findOneAndUpdate(sc, filter, update, dest, opt)
return t.closeSessionAutomatically(sc, *opt.DisableAutoCloseSession, *opt.DisableAutoRollbackSession, err)
})
}
return err
}
// Find executes a find command, if successful it returns the corresponding documents in the collection in the dest
// parameter with return error nil. Otherwise, it returns corresponding error.
//
// The filter parameter must be a document containing query operators and can be used to select which documents are
// included in the result. If the filter does not match any document, the dest parameter will not be modified.
//
// The dest parameter must be a pointer to the return expected by the operation, it is important to have the
// database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.Find documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.
func (t *Template) Find(ctx context.Context, filter, dest any, opts ...*option.Find) error {
return t.find(ctx, filter, dest, opts...)
}
// FindAll execute a search command. This is equivalent to running Find(ctx, bson.D{}, dest, opts...).
//
// The dest parameter must be a pointer to the return expected by the operation, it is important to have the
// database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.Find documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.
func (t *Template) FindAll(ctx context.Context, dest any, opts ...*option.Find) error {
return t.find(ctx, bson.D{}, dest, opts...)
}
// FindPageable executes a find command, if successful, returns the paginated documents in the
// corresponding PageResult structure in the collection on the target parameter with null return error.
// Otherwise, it will return the corresponding error.
//
// The filter parameter must be a document containing query operators and can be used to select which documents are
// included in the result. It cannot be nil. If the filter does not match any document, the return structure columns
// will be empty.
//
// The opts parameter can be used to specify options for the operation (see the option.FindPageable documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.
func (t *Template) FindPageable(ctx context.Context, filter any, input PageInput, opts ...*option.FindPageable) (
*PageResult, error) {
if helper.IsNotStruct(input.Ref) {
return nil, errors.New("mongo: input.Ref need to be structure")
}
_, collection, err := t.getMongoInfosByAny(input.Ref)
if helper.IsNotNil(err) {
return nil, err
}
opt := option.MergeFindPageableByParams(opts)
skip := input.Page * input.PageSize
cursor, err := collection.Find(ctx, filter, &options.FindOptions{
AllowDiskUse: opt.AllowDiskUse,
AllowPartialResults: opt.AllowPartialResults,
BatchSize: opt.BatchSize,
Collation: option.ParseCollationMongoOptions(opt.Collation),
Comment: opt.Comment,
CursorType: option.ParseCursorType(opt.CursorType),
Hint: opt.Hint,
Limit: &input.PageSize,
Max: opt.Max,
MaxAwaitTime: opt.MaxAwaitTime,
MaxTime: opt.MaxTime,
Min: opt.Min,
NoCursorTimeout: opt.NoCursorTimeout,
Projection: opt.Projection,
ReturnKey: opt.ReturnKey,
ShowRecordID: opt.ShowRecordID,
Skip: &skip,
Sort: input.Sort,
Let: opt.Let,
})
defer t.closeCursor(ctx, cursor)
if helper.IsNil(err) {
dest := reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(input.Ref)), 0, 0).Interface()
err = cursor.All(ctx, &dest)
if helper.IsNil(err) {
countTotal, _ := collection.CountDocuments(ctx, filter)
return newPageResult(input, dest, countTotal), nil
}
}
return nil, err
}
// Exists executes the count command, if the quantity is greater than 0 with a limit of 1, true is returned,
// otherwise false is returned.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.Exists documentation).
func (t *Template) Exists(ctx context.Context, filter, ref any, opts ...*option.Exists) (bool, error) {
return t.exists(ctx, filter, ref, opts...)
}
// ExistsById executes a count command whose _id value matches the ID given in the collection.
// This is equivalent to running Exists(ctx, bson.D{{"_id", id}}, dest, opts...).
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.Exists documentation).
func (t *Template) ExistsById(ctx context.Context, id, ref any, opts ...*option.Exists) (bool, error) {
return t.exists(ctx, bson.D{{"_id", id}}, ref, opts...)
}
// Aggregate executes a find command, if successful it returns the corresponding documents in the collection in the dest
// parameter with return error nil. Otherwise, it returns corresponding error.
//
// The pipeline parameter must be an array of documents, each representing an aggregation stage. The pipeline cannot
// be nil but can be empty. The stage documents must all be non-nil. For a pipeline of bson.D documents, the
// Pipeline type can be used. See
// https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/#db-collection-aggregate-stages for a list of
// valid stages in aggregations.
//
// The dest parameter must be a pointer to the return expected by the operation, it is important to have the
// database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.Aggregate documentation.)
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/aggregate/.
func (t *Template) Aggregate(ctx context.Context, pipeline any, dest any, opts ...*option.Aggregate) error {
if helper.IsNotPointer(dest) {
return ErrDestIsNotPointer
}
_, collection, err := t.getMongoInfosByAny(dest)
if helper.IsNotNil(err) {
return err
}
opt := option.MergeAggregateByParams(opts)
cursor, err := collection.Aggregate(ctx, pipeline, &options.AggregateOptions{
AllowDiskUse: opt.AllowDiskUse,
BatchSize: opt.BatchSize,
BypassDocumentValidation: opt.BypassDocumentValidation,
Collation: option.ParseCollationMongoOptions(opt.Collation),
MaxTime: opt.MaxTime,
MaxAwaitTime: opt.MaxAwaitTime,
Comment: opt.Comment,
Hint: opt.Hint,
Let: opt.Let,
Custom: opt.Custom,
})
defer t.closeCursor(ctx, cursor)
if helper.IsNil(err) {
err = cursor.All(ctx, dest)
}
return err
}
// CountDocuments returns the number of documents in the collection. For a fast count of the documents in the
// collection, see the EstimatedDocumentCount method.
//
// The filter parameter must be a document and can be used to select which documents contribute to the count. It
// cannot be nil. An empty document (e.g. bson.D{}) should be used to count all documents in the collection. This will
// result in a full collection scan.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.Count documentation).
func (t *Template) CountDocuments(ctx context.Context, filter, ref any, opts ...*option.Count) (int64, error) {
return t.countDocuments(ctx, filter, ref, opts...)
}
// EstimatedDocumentCount executes a count command and returns an estimate of the number of documents in the collection
// using collection metadata.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the option.EstimatedDocumentCount
// documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/count/.
func (t *Template) EstimatedDocumentCount(ctx context.Context, ref any, opts ...*option.EstimatedDocumentCount) (int64,
error) {
_, collection, err := t.getMongoInfosByAny(ref)
if helper.IsNotNil(err) {
return 0, err
}
opt := option.MergeEstimatedDocumentCountByParams(opts)
count, err := collection.EstimatedDocumentCount(ctx, &options.EstimatedDocumentCountOptions{
Comment: opt.Comment,
MaxTime: opt.MaxTime,
})
return count, err
}
// Distinct executes a distinct command to find the unique values for a specified field in the collection.
//
// The fieldName parameter specifies the field name for which distinct values should be returned.
//
// The filter parameter must be a document containing query operators and can be used to select which documents are
// considered. It cannot be nil. An empty document (e.g. bson.D{}) should be used to select all documents.
//
// The dest parameter must be a pointer to the return expected by the operation.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The opts parameter can be used to specify options for the operation (see the options.DistinctOptions documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/distinct/.
func (t *Template) Distinct(ctx context.Context, fieldName string, filter, dest, ref any, opts ...*option.Distinct) error {
if helper.IsNotPointer(dest) {
return ErrDestIsNotPointer
}
opt := option.MergeDistinctByParams(opts)
_, collection, err := t.getMongoInfosByAny(ref)
if helper.IsNotNil(err) {
return err
}
result, err := collection.Distinct(ctx, fieldName, filter, &options.DistinctOptions{
Collation: option.ParseCollationMongoOptions(opt.Collation),
Comment: opt.Comment,
MaxTime: opt.MaxTime,
})
if helper.IsNil(err) {
err = helper.ConvertToDest(result, dest)
}
return err
}
// Watch returns a change stream for all changes on the deployment. See
// https://www.mongodb.com/docs/manual/changeStreams/ for more information about change streams.
//
// The client must be configured with read concern majority or no read concern for a change stream to be created
// successfully.
//
// The pipeline parameter must be an array of documents, each representing a pipeline stage. The pipeline cannot be
// nil or empty. The stage documents must all be non-nil. See https://www.mongodb.com/docs/manual/changeStreams/ for a list
// of pipeline stages that can be used with change streams. For a pipeline of bson.D documents, the mongo.Pipeline{}
// type can be used.
//
// The opts parameter can be used to specify options for change stream creation (see the option.Watch documentation).
func (t *Template) Watch(ctx context.Context, pipeline any, opts ...*option.Watch) (*mongo.ChangeStream, error) {
opt := option.MergeWatchByParams(opts)
var watchChangeEvents *mongo.ChangeStream
var err error
optionsChangeStream := &options.ChangeStreamOptions{
BatchSize: opt.BatchSize,
Collation: option.ParseCollationMongoOptions(opt.Collation),
Comment: opt.Comment,
FullDocument: option.ParseFullDocument(opt.FullDocument),
FullDocumentBeforeChange: option.ParseFullDocument(opt.FullDocumentBeforeChange),
MaxAwaitTime: opt.MaxAwaitTime,
ResumeAfter: opt.ResumeAfter,
ShowExpandedEvents: opt.ShowExpandedEvents,
StartAtOperationTime: opt.StartAtOperationTime,
StartAfter: opt.StartAfter,
Custom: opt.Custom,
CustomPipeline: opt.CustomPipeline,
}
if helper.IsNotEmpty(opt.DatabaseName) {
database := t.client.Database(opt.DatabaseName)
if helper.IsNotEmpty(opt.CollectionName) {
watchChangeEvents, err = database.Collection(opt.CollectionName).Watch(ctx, pipeline, optionsChangeStream)
} else {
watchChangeEvents, err = database.Watch(ctx, pipeline, optionsChangeStream)
}
} else {
watchChangeEvents, err = t.client.Watch(ctx, pipeline, optionsChangeStream)
}
return watchChangeEvents, err
}
// WatchWithHandler is a function that facilitates the reading of watch events, it triggers the Watch function and
// when an event occurs it reads this event transforming all the data obtained by mongoDB into a Context, right
// after this conversion, we call the handler parameter passing the context with all the information, so you can
// process it in the way you see fit.
//
// The opts parameter can be used to specify options for change stream creation (see the option.WatchWithHandler
// documentation).
func (t *Template) WatchWithHandler(ctx context.Context, pipeline any, handler EventHandler,
opts ...*option.WatchWithHandler) error {
opt := option.MergeWatchHandlerByParams(opts)
watchEventChanges, err := t.Watch(ctx, pipeline, &option.Watch{
DatabaseName: opt.DatabaseName,
CollectionName: opt.CollectionName,
BatchSize: opt.BatchSize,
Collation: opt.Collation,
Comment: opt.Comment,
FullDocument: opt.FullDocument,
FullDocumentBeforeChange: opt.FullDocumentBeforeChange,
MaxAwaitTime: opt.MaxAwaitTime,
ResumeAfter: opt.ResumeAfter,
ShowExpandedEvents: opt.ShowExpandedEvents,
StartAtOperationTime: opt.StartAtOperationTime,
StartAfter: opt.StartAfter,
Custom: opt.Custom,
CustomPipeline: opt.CustomPipeline,
})
if helper.IsNotNil(err) {
return err
}
for watchEventChanges.Next(ctx) {
var event Event
_ = watchEventChanges.Decode(&event)
processNextEvent(handler, event, opt)
}
_ = watchEventChanges.Close(ctx)
return nil
}
// DropCollection drops the collection on the server. This method ignores "namespace not found" errors,
// so it is safe to drop a collection that does not exist on the server.
//
// The ref parameter must be the collection structure with database and collection tags configured.
func (t *Template) DropCollection(ctx context.Context, ref any) error {
_, collection, err := t.getMongoInfosByAny(ref)
if helper.IsNotNil(err) {
return err
}
return collection.Drop(ctx)
}
// DropDatabase drops the database on the server. This method ignores "namespace not found" errors,
// so it is safe to drop a database that does not exist on the server.
//
// The ref parameter must be the collection structure with database and collection tags configured.
func (t *Template) DropDatabase(ctx context.Context, ref any) error {
database, _, err := t.getMongoInfosByAny(ref)
if helper.IsNotNil(err) {
return err
}
return database.Drop(ctx)
}
// CreateOneIndex executes a createIndexes command to create an index on the collection and returns the name of the new
// index. See the CreateManyIndex documentation for more information and an example. For this function's response,
// the name of the index is returned as a string, and if an error occurs, it is returned in the second return parameter
//
// The opts parameter can be used to specify options for this operation (see the option.Index documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/createIndexes/.
func (t *Template) CreateOneIndex(ctx context.Context, input IndexInput) (string, error) {
return t.createOneIndex(ctx, input)
}
// CreateManyIndex executes a createIndexes command to create multiple indexes on the collection and returns the names of
// the new indexes.
//
// For each IndexInput in the models parameter, the index name can be specified via the Options field. If a name is not
// given, it will be generated from the Keys document.
//
// The opts parameter can be used to specify options for this operation (see the option.Index documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/createIndexes/.
func (t *Template) CreateManyIndex(ctx context.Context, inputs []IndexInput) ([]string, error) {
return t.createManyIndex(ctx, inputs)
}
// DropOneIndex executes a dropIndexes operation to drop an index on the collection. If the operation succeeds, this returns
// a BSON document in the form {nIndexesWas: <int32>}. The "nIndexesWas" field in the response contains the number of
// indexes that existed prior to the drop.
//
// The name parameter should be the name of the index to drop. If the name is "*", ErrMultipleIndexDrop will be returned
// without running the command because doing so would drop all indexes.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The opts parameter can be used to specify options for this operation (see the option.DropIndex documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/dropIndexes/.
func (t *Template) DropOneIndex(ctx context.Context, name string, ref any, opts ...*option.DropIndex) error {
opt := option.MergeDropIndexByParams(opts)
_, collection, err := t.getMongoInfosByAny(ref)
if helper.IsNil(err) {
_, err = collection.Indexes().DropOne(ctx, name, &options.DropIndexesOptions{MaxTime: opt.MaxTime})
}
return err
}
// DropAllIndexes executes a dropIndexes operation to drop all indexes on the collection. If the operation succeeds, this
// returns a BSON document in the form {nIndexesWas: <int32>}. The "nIndexesWas" field in the response contains the
// number of indexes that existed prior to the drop.
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// The opts parameter can be used to specify options for this operation (see the option.DropIndex documentation).
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/dropIndexes/.
func (t *Template) DropAllIndexes(ctx context.Context, ref any, opts ...*option.DropIndex) error {
opt := option.MergeDropIndexByParams(opts)
_, collection, err := t.getMongoInfosByAny(ref)
if helper.IsNil(err) {
_, err = collection.Indexes().DropAll(ctx, &options.DropIndexesOptions{MaxTime: opt.MaxTime})
}
return err
}
// ListIndexes executes a listIndexes command and returns a cursor over the indexes in the collection.
//
// The opts parameter can be used to specify options for this operation (see the option.ListIndexes documentation).
//
// The ref parameter must be the collection structure with database and collection tags configured.
//
// For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/listIndexes/.
func (t *Template) ListIndexes(ctx context.Context, ref any, opts ...*option.ListIndexes) ([]IndexResult, error) {
_, collection, err := t.getMongoInfosByAny(ref)
if helper.IsNotNil(err) {
return nil, err
}
opt := option.MergeListIndexesByParams(opts)
cursor, err := collection.Indexes().List(ctx, &options.ListIndexesOptions{
BatchSize: opt.BatchSize,
MaxTime: opt.MaxTime,
})
defer t.closeCursor(ctx, cursor)
var results []IndexResult
if helper.IsNil(err) {
err = cursor.All(ctx, &results)
}
return results, err
}
// ListIndexSpecifications executes a List command and returns a slice of returned IndexSpecifications.
//
// The ref parameter must be the collection structure with database and collection tags configured.
func (t *Template) ListIndexSpecifications(ctx context.Context, ref any, opts ...*option.ListIndexes) (
[]IndexSpecification, error) {
_, collection, err := t.getMongoInfosByAny(ref)
if helper.IsNotNil(err) {
return nil, err
}
opt := option.MergeListIndexesByParams(opts)
mongoResult, err := collection.Indexes().ListSpecifications(ctx, &options.ListIndexesOptions{
BatchSize: opt.BatchSize,
MaxTime: opt.MaxTime,
})
var result []IndexSpecification
for _, v := range mongoResult {
if helper.IsNotNil(v) {
result = append(result, IndexSpecification{
Name: v.Name,
Namespace: v.Namespace,
KeysDocument: v.KeysDocument,
Version: v.Version,
ExpireAfterSeconds: v.ExpireAfterSeconds,