Skip to content

Commit b22781b

Browse files
authored
GODRIVER-3522 Add rawData option to find/findAndModify, update/replace, and collection bulkWrite (#2123)
1 parent 5e7fa80 commit b22781b

File tree

13 files changed

+375
-10
lines changed

13 files changed

+375
-10
lines changed

internal/integration/unified/collection_operation_execution.go

Lines changed: 30 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
}
@@ -873,6 +878,11 @@ func executeFindOneAndDelete(ctx context.Context, operation *operation) (*operat
873878
opts.SetSort(val.Document())
874879
case "let":
875880
opts.SetLet(val.Document())
881+
case "rawData":
882+
err = xoptions.SetInternalFindOneAndDeleteOptions(opts, key, val.Boolean())
883+
if err != nil {
884+
return nil, err
885+
}
876886
default:
877887
return nil, fmt.Errorf("unrecognized findOneAndDelete option %q", key)
878888
}
@@ -955,6 +965,11 @@ func executeFindOneAndReplace(ctx context.Context, operation *operation) (*opera
955965
opts.SetSort(val.Document())
956966
case "upsert":
957967
opts.SetUpsert(val.Boolean())
968+
case "rawData":
969+
err = xoptions.SetInternalFindOneAndReplaceOptions(opts, key, val.Boolean())
970+
if err != nil {
971+
return nil, err
972+
}
958973
default:
959974
return nil, fmt.Errorf("unrecognized findOneAndReplace option %q", key)
960975
}
@@ -1047,6 +1062,11 @@ func executeFindOneAndUpdate(ctx context.Context, operation *operation) (*operat
10471062
}
10481063
case "upsert":
10491064
opts.SetUpsert(val.Boolean())
1065+
case "rawData":
1066+
err = xoptions.SetInternalFindOneAndUpdateOptions(opts, key, val.Boolean())
1067+
if err != nil {
1068+
return nil, err
1069+
}
10501070
default:
10511071
return nil, fmt.Errorf("unrecognized findOneAndUpdate option %q", key)
10521072
}
@@ -1343,6 +1363,11 @@ func executeReplaceOne(ctx context.Context, operation *operation) (*operationRes
13431363
opts.SetUpsert(val.Boolean())
13441364
case "let":
13451365
opts.SetLet(val.Document())
1366+
case "rawData":
1367+
err = xoptions.SetInternalReplaceOptions(opts, key, val.Boolean())
1368+
if err != nil {
1369+
return nil, err
1370+
}
13461371
default:
13471372
return nil, fmt.Errorf("unrecognized replaceOne option %q", key)
13481373
}
@@ -1541,6 +1566,11 @@ func createFindCursor(ctx context.Context, operation *operation) (*cursorResult,
15411566
case "maxAwaitTimeMS":
15421567
maxAwaitTimeMS := time.Duration(val.Int32()) * time.Millisecond
15431568
opts.SetMaxAwaitTime(maxAwaitTimeMS)
1569+
case "rawData":
1570+
err = xoptions.SetInternalFindOptions(opts, key, val.Boolean())
1571+
if err != nil {
1572+
return nil, err
1573+
}
15441574
default:
15451575
return nil, fmt.Errorf("unrecognized find option %q", key)
15461576
}

internal/integration/unified/crud_helpers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"go.mongodb.org/mongo-driver/v2/bson"
1313
"go.mongodb.org/mongo-driver/v2/internal/bsonutil"
1414
"go.mongodb.org/mongo-driver/v2/mongo/options"
15+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/xoptions"
1516
)
1617

1718
// newMissingArgumentError creates an error to convey that an argument that is required to run an operation is missing
@@ -67,6 +68,11 @@ func createUpdateManyArguments(args bson.Raw) (*updateArguments, *options.Update
6768
}
6869
case "upsert":
6970
opts.SetUpsert(val.Boolean())
71+
case "rawData":
72+
err := xoptions.SetInternalUpdateManyOptions(opts, key, val.Boolean())
73+
if err != nil {
74+
return nil, nil, err
75+
}
7076
default:
7177
return nil, nil, fmt.Errorf("unrecognized update option %q", key)
7278
}
@@ -125,6 +131,11 @@ func createUpdateOneArguments(args bson.Raw) (*updateArguments, *options.UpdateO
125131
opts.SetUpsert(val.Boolean())
126132
case "sort":
127133
opts.SetSort(val.Document())
134+
case "rawData":
135+
err := xoptions.SetInternalUpdateOneOptions(opts, key, val.Boolean())
136+
if err != nil {
137+
return nil, nil, err
138+
}
128139
default:
129140
return nil, nil, fmt.Errorf("unrecognized update option %q", key)
130141
}

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: 33 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

@@ -700,6 +705,11 @@ func (coll *Collection) updateOrReplace(
700705
}
701706
op = op.Comment(comment)
702707
}
708+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
709+
if rawData, ok := rawDataOpt.(bool); ok {
710+
op = op.RawData(rawData)
711+
}
712+
}
703713
retry := driver.RetryNone
704714
// retryable writes are only enabled updateOne/replaceOne operations
705715
if !multi && coll.client.retryWrites {
@@ -794,6 +804,7 @@ func (coll *Collection) UpdateOne(
794804
Hint: args.Hint,
795805
Upsert: args.Upsert,
796806
Let: args.Let,
807+
Internal: args.Internal,
797808
}
798809

799810
return coll.updateOrReplace(ctx, f, update, false, rrOne, true, args.Sort, updateOptions)
@@ -884,6 +895,7 @@ func (coll *Collection) ReplaceOne(
884895
Hint: args.Hint,
885896
Let: args.Let,
886897
Comment: args.Comment,
898+
Internal: args.Internal,
887899
}
888900

889901
return coll.updateOrReplace(ctx, f, r, false, rrOne, false, args.Sort, updateOptions)
@@ -1536,6 +1548,11 @@ func (coll *Collection) find(
15361548
}
15371549
op.Sort(sort)
15381550
}
1551+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
1552+
if rawData, ok := rawDataOpt.(bool); ok {
1553+
op = op.RawData(rawData)
1554+
}
1555+
}
15391556
retry := driver.RetryNone
15401557
if coll.client.retryReads {
15411558
retry = driver.RetryOncePerCommand
@@ -1569,6 +1586,7 @@ func newFindArgsFromFindOneArgs(args *options.FindOneOptions) *options.FindOptio
15691586
v.ShowRecordID = args.ShowRecordID
15701587
v.Skip = args.Skip
15711588
v.Sort = args.Sort
1589+
v.Internal = args.Internal
15721590
}
15731591
return v
15741592
}
@@ -1731,6 +1749,11 @@ func (coll *Collection) FindOneAndDelete(
17311749
}
17321750
op = op.Let(let)
17331751
}
1752+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
1753+
if rawData, ok := rawDataOpt.(bool); ok {
1754+
op = op.RawData(rawData)
1755+
}
1756+
}
17341757

17351758
return coll.findAndModify(ctx, op)
17361759
}
@@ -1828,6 +1851,11 @@ func (coll *Collection) FindOneAndReplace(
18281851
}
18291852
op = op.Let(let)
18301853
}
1854+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
1855+
if rawData, ok := rawDataOpt.(bool); ok {
1856+
op = op.RawData(rawData)
1857+
}
1858+
}
18311859

18321860
return coll.findAndModify(ctx, op)
18331861
}
@@ -1937,6 +1965,11 @@ func (coll *Collection) FindOneAndUpdate(
19371965
}
19381966
op = op.Let(let)
19391967
}
1968+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
1969+
if rawData, ok := rawDataOpt.(bool); ok {
1970+
op = op.RawData(rawData)
1971+
}
1972+
}
19401973

19411974
return coll.findAndModify(ctx, op)
19421975
}

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.

mongo/options/findoptions.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ package options
88

99
import (
1010
"time"
11+
12+
"go.mongodb.org/mongo-driver/v2/internal/optionsutil"
1113
)
1214

1315
// FindOptions represents arguments that can be used to configure a Find
@@ -35,6 +37,10 @@ type FindOptions struct {
3537
Let interface{}
3638
Limit *int64
3739
NoCursorTimeout *bool
40+
41+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
42+
// release.
43+
Internal optionsutil.Options
3844
}
3945

4046
// FindOptionsBuilder represents functional options that configure an Findopts.
@@ -285,6 +291,10 @@ type FindOneOptions struct {
285291
ShowRecordID *bool
286292
Skip *int64
287293
Sort interface{}
294+
295+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
296+
// release.
297+
Internal optionsutil.Options
288298
}
289299

290300
// FindOneOptionsBuilder represents functional options that configure an
@@ -450,6 +460,10 @@ type FindOneAndReplaceOptions struct {
450460
Upsert *bool
451461
Hint interface{}
452462
Let interface{}
463+
464+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
465+
// release.
466+
Internal optionsutil.Options
453467
}
454468

455469
// FindOneAndReplaceOptionsBuilder contains options to perform a findAndModify
@@ -611,6 +625,10 @@ type FindOneAndUpdateOptions struct {
611625
Upsert *bool
612626
Hint interface{}
613627
Let interface{}
628+
629+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
630+
// release.
631+
Internal optionsutil.Options
614632
}
615633

616634
// FindOneAndUpdateOptionsBuilder contains options to configure a
@@ -782,6 +800,10 @@ type FindOneAndDeleteOptions struct {
782800
Sort interface{}
783801
Hint interface{}
784802
Let interface{}
803+
804+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
805+
// release.
806+
Internal optionsutil.Options
785807
}
786808

787809
// FindOneAndDeleteOptionsBuilder contains options to configure delete

mongo/options/replaceoptions.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
// ReplaceOptions represents arguments that can be used to configure a ReplaceOne
1012
// operation.
1113
//
@@ -18,6 +20,10 @@ type ReplaceOptions struct {
1820
Upsert *bool
1921
Let interface{}
2022
Sort 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
// ReplaceOptionsBuilder contains options to configure replace operations. Each

mongo/options/updateoptions.go

Lines changed: 10 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
// UpdateOneOptions represents arguments that can be used to configure UpdateOne
1012
// operations.
1113
//
@@ -19,6 +21,10 @@ type UpdateOneOptions struct {
1921
Upsert *bool
2022
Let interface{}
2123
Sort interface{}
24+
25+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
26+
// release.
27+
Internal optionsutil.Options
2228
}
2329

2430
// UpdateOneOptionsBuilder contains options to configure UpdateOne operations.
@@ -164,6 +170,10 @@ type UpdateManyOptions struct {
164170
Hint interface{}
165171
Upsert *bool
166172
Let interface{}
173+
174+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
175+
// release.
176+
Internal optionsutil.Options
167177
}
168178

169179
// UpdateManyOptionsBuilder contains options to configure UpdateMany operations.

0 commit comments

Comments
 (0)