Skip to content

Ability to set WriteConcern at BulkWrite operation level #2144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions mongo/options/bulkwriteoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,6 +20,7 @@ type BulkWriteOptions struct {
Comment interface{}
Ordered *bool
Let interface{}
WriteConcern *writeconcern.WriteConcern
}

// BulkWriteOptionsBuilder contains options to configure bulk write operations.
Expand Down Expand Up @@ -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
}