diff --git a/mongo/collection.go b/mongo/collection.go index 6acadc285e..0b807bfea6 100644 --- a/mongo/collection.go +++ b/mongo/collection.go @@ -211,7 +211,18 @@ func (coll *Collection) BulkWrite(ctx context.Context, models []WriteModel, return nil, err } - wc := coll.writeConcern + // Ensure opts have the default case at the front. + opts = append([]options.Lister[options.BulkWriteOptions]{options.BulkWrite()}, opts...) + args, err := mongoutil.NewOptions(opts...) + if err != nil { + return nil, err + } + + wc := args.WriteConcern + if wc == nil { + wc = coll.writeConcern + } + if sess.TransactionRunning() { wc = nil } @@ -227,13 +238,6 @@ func (coll *Collection) BulkWrite(ctx context.Context, models []WriteModel, } } - // Ensure opts have the default case at the front. - opts = append([]options.Lister[options.BulkWriteOptions]{options.BulkWrite()}, opts...) - args, err := mongoutil.NewOptions(opts...) - if err != nil { - return nil, err - } - op := bulkWrite{ comment: args.Comment, ordered: args.Ordered, diff --git a/mongo/options/bulkwriteoptions.go b/mongo/options/bulkwriteoptions.go index 186e83a0c5..48f5b2195e 100644 --- a/mongo/options/bulkwriteoptions.go +++ b/mongo/options/bulkwriteoptions.go @@ -6,6 +6,8 @@ package options +import "go.mongodb.org/mongo-driver/v2/mongo/writeconcern" + // DefaultOrdered is the default value for the Ordered option in BulkWriteOptions. var DefaultOrdered = true @@ -18,6 +20,7 @@ type BulkWriteOptions struct { Comment interface{} Ordered *bool Let interface{} + WriteConcern *writeconcern.WriteConcern } // BulkWriteOptionsBuilder contains options to configure bulk write operations. @@ -92,3 +95,17 @@ func (b *BulkWriteOptionsBuilder) SetLet(let interface{}) *BulkWriteOptionsBuild return b } + +// SetWriteConcern sets the WriteConcern for the BulkWrite operation, specifying the acknowledgment level required. +// WriteConcern is the write concern to use for operations executed on the BulkWrite. +// The default value is nil, which means that the write +// concern of the Collection will be used to configure the BulkWrite WriteConcern. +func (b *BulkWriteOptionsBuilder) SetWriteConcern(wc *writeconcern.WriteConcern) *BulkWriteOptionsBuilder { + b.Opts = append(b.Opts, func(opts *BulkWriteOptions) error { + opts.WriteConcern = wc + + return nil + }) + + return b +}