Skip to content

Commit bccaed5

Browse files
committed
add bulkWrite
1 parent fdef4af commit bccaed5

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

internal/integration/unified/collection_operation_execution.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ func executeBulkWrite(ctx context.Context, operation *operation) (*operationResu
131131
}
132132
case "let":
133133
opts.SetLet(val.Document())
134+
case "rawData":
135+
err = xoptions.SetInternalBulkWriteOptions(opts, key, val.Boolean())
136+
if err != nil {
137+
return nil, err
138+
}
134139
default:
135140
return nil, fmt.Errorf("unrecognized bulkWrite option %q", key)
136141
}

mongo/bulk_write.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type bulkWrite struct {
3939
writeConcern *writeconcern.WriteConcern
4040
result BulkWriteResult
4141
let interface{}
42+
rawData *bool
4243
}
4344

4445
func (bw *bulkWrite) execute(ctx context.Context) error {
@@ -209,6 +210,10 @@ func (bw *bulkWrite) runInsert(ctx context.Context, batch bulkWriteBatch) (opera
209210
}
210211
op = op.Retry(retry)
211212

213+
if bw.rawData != nil {
214+
op.RawData(*bw.rawData)
215+
}
216+
212217
err := op.Execute(ctx)
213218

214219
return op.Result(), err
@@ -282,6 +287,10 @@ func (bw *bulkWrite) runDelete(ctx context.Context, batch bulkWriteBatch) (opera
282287
}
283288
op = op.Retry(retry)
284289

290+
if bw.rawData != nil {
291+
op.RawData(*bw.rawData)
292+
}
293+
285294
err := op.Execute(ctx)
286295

287296
return op.Result(), err
@@ -415,6 +424,10 @@ func (bw *bulkWrite) runUpdate(ctx context.Context, batch bulkWriteBatch) (opera
415424
}
416425
op = op.Retry(retry)
417426

427+
if bw.rawData != nil {
428+
op.RawData(*bw.rawData)
429+
}
430+
418431
err := op.Execute(ctx)
419432

420433
return op.Result(), err

mongo/collection.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ func (coll *Collection) BulkWrite(ctx context.Context, models []WriteModel,
246246
writeConcern: wc,
247247
let: args.Let,
248248
}
249+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
250+
if rawData, ok := rawDataOpt.(bool); ok {
251+
op.rawData = &rawData
252+
}
253+
}
249254

250255
err = op.execute(ctx)
251256

mongo/options/bulkwriteoptions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package options
88

9+
import "go.mongodb.org/mongo-driver/v2/internal/optionsutil"
10+
911
// DefaultOrdered is the default value for the Ordered option in BulkWriteOptions.
1012
var DefaultOrdered = true
1113

@@ -18,6 +20,10 @@ type BulkWriteOptions struct {
1820
Comment interface{}
1921
Ordered *bool
2022
Let interface{}
23+
24+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
25+
// release.
26+
Internal optionsutil.Options
2127
}
2228

2329
// BulkWriteOptionsBuilder contains options to configure bulk write operations.

x/mongo/driver/xoptions/options.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@ func SetInternalAggregateOptions(a *options.AggregateOptionsBuilder, key string,
6565
return nil
6666
}
6767

68+
// SetInternalBulkWriteOptions sets internal options for BulkWriteOptions.
69+
func SetInternalBulkWriteOptions(a *options.BulkWriteOptionsBuilder, key string, option any) error {
70+
typeErrFunc := func(t string) error {
71+
return fmt.Errorf("unexpected type for %q: %T is not %s", key, option, t)
72+
}
73+
switch key {
74+
case "rawData":
75+
b, ok := option.(bool)
76+
if !ok {
77+
return typeErrFunc("bool")
78+
}
79+
a.Opts = append(a.Opts, func(opts *options.BulkWriteOptions) error {
80+
opts.Internal = optionsutil.WithValue(opts.Internal, key, b)
81+
return nil
82+
})
83+
default:
84+
return fmt.Errorf("unsupported option: %q", key)
85+
}
86+
return nil
87+
}
88+
6889
// SetInternalCountOptions sets internal options for CountOptions.
6990
func SetInternalCountOptions(a *options.CountOptionsBuilder, key string, option any) error {
7091
typeErrFunc := func(t string) error {

0 commit comments

Comments
 (0)