Skip to content

Commit fdef4af

Browse files
committed
add update/replace
1 parent 503f3d3 commit fdef4af

File tree

7 files changed

+117
-0
lines changed

7 files changed

+117
-0
lines changed

internal/integration/unified/collection_operation_execution.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,11 @@ func executeReplaceOne(ctx context.Context, operation *operation) (*operationRes
13581358
opts.SetUpsert(val.Boolean())
13591359
case "let":
13601360
opts.SetLet(val.Document())
1361+
case "rawData":
1362+
err = xoptions.SetInternalReplaceOptions(opts, key, val.Boolean())
1363+
if err != nil {
1364+
return nil, err
1365+
}
13611366
default:
13621367
return nil, fmt.Errorf("unrecognized replaceOne option %q", key)
13631368
}

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/collection.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,11 @@ func (coll *Collection) updateOrReplace(
700700
}
701701
op = op.Comment(comment)
702702
}
703+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
704+
if rawData, ok := rawDataOpt.(bool); ok {
705+
op = op.RawData(rawData)
706+
}
707+
}
703708
retry := driver.RetryNone
704709
// retryable writes are only enabled updateOne/replaceOne operations
705710
if !multi && coll.client.retryWrites {
@@ -794,6 +799,7 @@ func (coll *Collection) UpdateOne(
794799
Hint: args.Hint,
795800
Upsert: args.Upsert,
796801
Let: args.Let,
802+
Internal: args.Internal,
797803
}
798804

799805
return coll.updateOrReplace(ctx, f, update, false, rrOne, true, args.Sort, updateOptions)
@@ -884,6 +890,7 @@ func (coll *Collection) ReplaceOne(
884890
Hint: args.Hint,
885891
Let: args.Let,
886892
Comment: args.Comment,
893+
Internal: args.Internal,
887894
}
888895

889896
return coll.updateOrReplace(ctx, f, r, false, rrOne, false, args.Sort, updateOptions)

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.

x/mongo/driver/operation/update.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Update struct {
4646
serverAPI *driver.ServerAPIOptions
4747
let bsoncore.Document
4848
timeout *time.Duration
49+
rawData *bool
4950
logger *logger.Logger
5051
}
5152

@@ -203,6 +204,10 @@ func (u *Update) command(dst []byte, desc description.SelectedServer) ([]byte, e
203204
if u.let != nil {
204205
dst = bsoncore.AppendDocumentElement(dst, "let", u.let)
205206
}
207+
// Set rawData for 8.2+ servers.
208+
if u.rawData != nil && desc.WireVersion != nil && driverutil.VersionRangeIncludes(*desc.WireVersion, 27) {
209+
dst = bsoncore.AppendBooleanElement(dst, "rawData", *u.rawData)
210+
}
206211

207212
return dst, nil
208213
}
@@ -422,3 +427,13 @@ func (u *Update) Authenticator(authenticator driver.Authenticator) *Update {
422427
u.authenticator = authenticator
423428
return u
424429
}
430+
431+
// RawData sets the rawData to access timeseries data in the compressed format.
432+
func (u *Update) RawData(rawData bool) *Update {
433+
if u == nil {
434+
u = new(Update)
435+
}
436+
437+
u.rawData = &rawData
438+
return u
439+
}

x/mongo/driver/xoptions/options.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,66 @@ func SetInternalInsertOneOptions(a *options.InsertOneOptionsBuilder, key string,
316316
}
317317
return nil
318318
}
319+
320+
// SetInternalReplaceOptions sets internal options for ReplaceOptions.
321+
func SetInternalReplaceOptions(a *options.ReplaceOptionsBuilder, key string, option any) error {
322+
typeErrFunc := func(t string) error {
323+
return fmt.Errorf("unexpected type for %q: %T is not %s", key, option, t)
324+
}
325+
switch key {
326+
case "rawData":
327+
b, ok := option.(bool)
328+
if !ok {
329+
return typeErrFunc("bool")
330+
}
331+
a.Opts = append(a.Opts, func(opts *options.ReplaceOptions) error {
332+
opts.Internal = optionsutil.WithValue(opts.Internal, key, b)
333+
return nil
334+
})
335+
default:
336+
return fmt.Errorf("unsupported option: %q", key)
337+
}
338+
return nil
339+
}
340+
341+
// SetInternalUpdateManyOptions sets internal options for UpdateManyOptions.
342+
func SetInternalUpdateManyOptions(a *options.UpdateManyOptionsBuilder, key string, option any) error {
343+
typeErrFunc := func(t string) error {
344+
return fmt.Errorf("unexpected type for %q: %T is not %s", key, option, t)
345+
}
346+
switch key {
347+
case "rawData":
348+
b, ok := option.(bool)
349+
if !ok {
350+
return typeErrFunc("bool")
351+
}
352+
a.Opts = append(a.Opts, func(opts *options.UpdateManyOptions) error {
353+
opts.Internal = optionsutil.WithValue(opts.Internal, key, b)
354+
return nil
355+
})
356+
default:
357+
return fmt.Errorf("unsupported option: %q", key)
358+
}
359+
return nil
360+
}
361+
362+
// SetInternalUpdateOneOptions sets internal options for UpdateOneOptions.
363+
func SetInternalUpdateOneOptions(a *options.UpdateOneOptionsBuilder, key string, option any) error {
364+
typeErrFunc := func(t string) error {
365+
return fmt.Errorf("unexpected type for %q: %T is not %s", key, option, t)
366+
}
367+
switch key {
368+
case "rawData":
369+
b, ok := option.(bool)
370+
if !ok {
371+
return typeErrFunc("bool")
372+
}
373+
a.Opts = append(a.Opts, func(opts *options.UpdateOneOptions) error {
374+
opts.Internal = optionsutil.WithValue(opts.Internal, key, b)
375+
return nil
376+
})
377+
default:
378+
return fmt.Errorf("unsupported option: %q", key)
379+
}
380+
return nil
381+
}

0 commit comments

Comments
 (0)