Skip to content

Commit 5e7fa80

Browse files
authored
Add rawData option to basic CRUD commands (#2121)
1 parent 724d9e4 commit 5e7fa80

File tree

15 files changed

+370
-6
lines changed

15 files changed

+370
-6
lines changed

internal/integration/unified/collection_operation_execution.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"go.mongodb.org/mongo-driver/v2/mongo"
2020
"go.mongodb.org/mongo-driver/v2/mongo/options"
2121
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
22+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/xoptions"
2223
)
2324

2425
// This file contains helpers to execute collection operations.
@@ -75,6 +76,11 @@ func executeAggregate(ctx context.Context, operation *operation) (*operationResu
7576
pipeline = bsonutil.RawToInterfaces(bsonutil.RawArrayToDocuments(val.Array())...)
7677
case "let":
7778
opts.SetLet(val.Document())
79+
case "rawData":
80+
err = xoptions.SetInternalAggregateOptions(opts, key, val.Boolean())
81+
if err != nil {
82+
return nil, err
83+
}
7884
default:
7985
return nil, fmt.Errorf("unrecognized aggregate option %q", key)
8086
}
@@ -202,6 +208,11 @@ func executeCountDocuments(ctx context.Context, operation *operation) (*operatio
202208
return nil, fmt.Errorf("the maxTimeMS collection option is not supported")
203209
case "skip":
204210
opts.SetSkip(int64(val.Int32()))
211+
case "rawData":
212+
err = xoptions.SetInternalCountOptions(opts, key, val.Boolean())
213+
if err != nil {
214+
return nil, err
215+
}
205216
default:
206217
return nil, fmt.Errorf("unrecognized countDocuments option %q", key)
207218
}
@@ -433,6 +444,11 @@ func executeDeleteOne(ctx context.Context, operation *operation) (*operationResu
433444
opts.SetHint(hint)
434445
case "let":
435446
opts.SetLet(val.Document())
447+
case "rawData":
448+
err = xoptions.SetInternalDeleteOneOptions(opts, key, val.Boolean())
449+
if err != nil {
450+
return nil, err
451+
}
436452
default:
437453
return nil, fmt.Errorf("unrecognized deleteOne option %q", key)
438454
}
@@ -487,6 +503,11 @@ func executeDeleteMany(ctx context.Context, operation *operation) (*operationRes
487503
opts.SetHint(hint)
488504
case "let":
489505
opts.SetLet(val.Document())
506+
case "rawData":
507+
err = xoptions.SetInternalDeleteManyOptions(opts, key, val.Boolean())
508+
if err != nil {
509+
return nil, err
510+
}
490511
default:
491512
return nil, fmt.Errorf("unrecognized deleteMany option %q", key)
492513
}
@@ -545,6 +566,11 @@ func executeDistinct(ctx context.Context, operation *operation) (*operationResul
545566
// ensured an analogue exists, extend "skippedTestDescriptions" to avoid
546567
// this error.
547568
return nil, fmt.Errorf("the maxTimeMS collection option is not supported")
569+
case "rawData":
570+
err = xoptions.SetInternalDistinctOptions(opts, key, val.Boolean())
571+
if err != nil {
572+
return nil, err
573+
}
548574
default:
549575
return nil, fmt.Errorf("unrecognized distinct option %q", key)
550576
}
@@ -690,6 +716,11 @@ func executeEstimatedDocumentCount(ctx context.Context, operation *operation) (*
690716
// ensured an analogue exists, extend "skippedTestDescriptions" to avoid
691717
// this error.
692718
return nil, fmt.Errorf("the maxTimeMS collection option is not supported")
719+
case "rawData":
720+
err = xoptions.SetInternalEstimatedDocumentCountOptions(opts, key, val.Boolean())
721+
if err != nil {
722+
return nil, err
723+
}
693724
default:
694725
return nil, fmt.Errorf("unrecognized estimatedDocumentCount option %q", key)
695726
}
@@ -1062,6 +1093,11 @@ func executeInsertMany(ctx context.Context, operation *operation) (*operationRes
10621093
documents = bsonutil.RawToInterfaces(bsonutil.RawArrayToDocuments(val.Array())...)
10631094
case "ordered":
10641095
opts.SetOrdered(val.Boolean())
1096+
case "rawData":
1097+
err = xoptions.SetInternalInsertManyOptions(opts, key, val.Boolean())
1098+
if err != nil {
1099+
return nil, err
1100+
}
10651101
default:
10661102
return nil, fmt.Errorf("unrecognized insertMany option %q", key)
10671103
}
@@ -1112,6 +1148,11 @@ func executeInsertOne(ctx context.Context, operation *operation) (*operationResu
11121148
opts.SetBypassDocumentValidation(val.Boolean())
11131149
case "comment":
11141150
opts.SetComment(val)
1151+
case "rawData":
1152+
err = xoptions.SetInternalInsertOneOptions(opts, key, val.Boolean())
1153+
if err != nil {
1154+
return nil, err
1155+
}
11151156
default:
11161157
return nil, fmt.Errorf("unrecognized insertOne option %q", key)
11171158
}

mongo/collection.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"go.mongodb.org/mongo-driver/v2/bson"
1818
"go.mongodb.org/mongo-driver/v2/internal/csfle"
1919
"go.mongodb.org/mongo-driver/v2/internal/mongoutil"
20+
"go.mongodb.org/mongo-driver/v2/internal/optionsutil"
2021
"go.mongodb.org/mongo-driver/v2/internal/serverselector"
2122
"go.mongodb.org/mongo-driver/v2/mongo/options"
2223
"go.mongodb.org/mongo-driver/v2/mongo/readconcern"
@@ -324,6 +325,11 @@ func (coll *Collection) insert(
324325
if args.Ordered != nil {
325326
op = op.Ordered(*args.Ordered)
326327
}
328+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
329+
if rawData, ok := rawDataOpt.(bool); ok {
330+
op = op.RawData(rawData)
331+
}
332+
}
327333
retry := driver.RetryNone
328334
if coll.client.retryWrites {
329335
retry = driver.RetryOncePerCommand
@@ -375,6 +381,13 @@ func (coll *Collection) InsertOne(ctx context.Context, document interface{},
375381
if args.Comment != nil {
376382
imOpts.SetComment(args.Comment)
377383
}
384+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
385+
imOpts.Opts = append(imOpts.Opts, func(opts *options.InsertManyOptions) error {
386+
optionsutil.WithValue(opts.Internal, "rawData", rawDataOpt)
387+
388+
return nil
389+
})
390+
}
378391
res, err := coll.insert(ctx, []interface{}{document}, imOpts)
379392

380393
rr, err := processWriteError(err)
@@ -534,6 +547,11 @@ func (coll *Collection) delete(
534547
}
535548
op = op.Let(let)
536549
}
550+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
551+
if rawData, ok := rawDataOpt.(bool); ok {
552+
op = op.RawData(rawData)
553+
}
554+
}
537555

538556
// deleteMany cannot be retried
539557
retryMode := driver.RetryNone
@@ -575,6 +593,7 @@ func (coll *Collection) DeleteOne(
575593
Comment: args.Comment,
576594
Hint: args.Hint,
577595
Let: args.Let,
596+
Internal: args.Internal,
578597
}
579598

580599
return coll.delete(ctx, filter, true, rrOne, deleteOptions)
@@ -1036,6 +1055,11 @@ func aggregate(a aggregateParams, opts ...options.Lister[options.AggregateOption
10361055
}
10371056
op.CustomOptions(customOptions)
10381057
}
1058+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
1059+
if rawData, ok := rawDataOpt.(bool); ok {
1060+
op = op.RawData(rawData)
1061+
}
1062+
}
10391063

10401064
retry := driver.RetryNone
10411065
if a.retryRead && !hasOutputStage {
@@ -1124,6 +1148,11 @@ func (coll *Collection) CountDocuments(ctx context.Context, filter interface{},
11241148
}
11251149
op.Hint(hintVal)
11261150
}
1151+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
1152+
if rawData, ok := rawDataOpt.(bool); ok {
1153+
op = op.RawData(rawData)
1154+
}
1155+
}
11271156
retry := driver.RetryNone
11281157
if coll.client.retryReads {
11291158
retry = driver.RetryOncePerCommand
@@ -1205,6 +1234,11 @@ func (coll *Collection) EstimatedDocumentCount(
12051234
}
12061235
op = op.Comment(comment)
12071236
}
1237+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
1238+
if rawData, ok := rawDataOpt.(bool); ok {
1239+
op = op.RawData(rawData)
1240+
}
1241+
}
12081242

12091243
retry := driver.RetryNone
12101244
if coll.client.retryReads {
@@ -1294,6 +1328,11 @@ func (coll *Collection) Distinct(
12941328
}
12951329
op.Hint(hint)
12961330
}
1331+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
1332+
if rawData, ok := rawDataOpt.(bool); ok {
1333+
op = op.RawData(rawData)
1334+
}
1335+
}
12971336
retry := driver.RetryNone
12981337
if coll.client.retryReads {
12991338
retry = driver.RetryOncePerCommand

mongo/options/aggregateoptions.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"go.mongodb.org/mongo-driver/v2/bson"
13+
"go.mongodb.org/mongo-driver/v2/internal/optionsutil"
1314
)
1415

1516
// AggregateOptions represents arguments that can be used to configure an
@@ -26,6 +27,10 @@ type AggregateOptions struct {
2627
Hint interface{}
2728
Let interface{}
2829
Custom bson.M
30+
31+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
32+
// release.
33+
Internal optionsutil.Options
2934
}
3035

3136
// AggregateOptionsBuilder contains options to configure aggregate operations.

mongo/options/countoptions.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
// CountOptions represents arguments that can be used to configure a
1012
// CountDocuments operation.
1113
//
@@ -16,6 +18,10 @@ type CountOptions struct {
1618
Hint interface{}
1719
Limit *int64
1820
Skip *int64
21+
22+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
23+
// release.
24+
Internal optionsutil.Options
1925
}
2026

2127
// CountOptionsBuilder contains options to configure count operations. Each

mongo/options/deleteoptions.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
// DeleteOneOptions represents arguments that can be used to configure DeleteOne
1012
// operations.
1113
//
@@ -15,6 +17,10 @@ type DeleteOneOptions struct {
1517
Comment interface{}
1618
Hint interface{}
1719
Let interface{}
20+
21+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
22+
// release.
23+
Internal optionsutil.Options
1824
}
1925

2026
// DeleteOneOptionsBuilder contains options to configure DeleteOne operations. Each
@@ -102,6 +108,10 @@ type DeleteManyOptions struct {
102108
Comment interface{}
103109
Hint interface{}
104110
Let interface{}
111+
112+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
113+
// release.
114+
Internal optionsutil.Options
105115
}
106116

107117
// DeleteManyOptionsBuilder contains options to configure DeleteMany operations.

mongo/options/distinctoptions.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
// DistinctOptions represents arguments that can be used to configure a Distinct
1012
// operation.
1113
//
@@ -14,6 +16,10 @@ type DistinctOptions struct {
1416
Collation *Collation
1517
Comment interface{}
1618
Hint interface{}
19+
20+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
21+
// release.
22+
Internal optionsutil.Options
1723
}
1824

1925
// DistinctOptionsBuilder contains options to configure distinct operations. Each

mongo/options/estimatedcountoptions.go

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

77
package options
88

9+
import "go.mongodb.org/mongo-driver/v2/internal/optionsutil"
10+
911
// EstimatedDocumentCountOptions represents arguments that can be used to configure
1012
// an EstimatedDocumentCount operation.
1113
//
1214
// See corresponding setter methods for documentation.
1315
type EstimatedDocumentCountOptions struct {
1416
Comment interface{}
17+
18+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
19+
// release.
20+
Internal optionsutil.Options
1521
}
1622

1723
// EstimatedDocumentCountOptionsBuilder contains options to estimate document

mongo/options/insertoptions.go

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

77
package options
88

9+
import "go.mongodb.org/mongo-driver/v2/internal/optionsutil"
10+
911
// InsertOneOptions represents arguments that can be used to configure an InsertOne
1012
// operation.
1113
//
1214
// See corresponding setter methods for documentation.
1315
type InsertOneOptions struct {
1416
BypassDocumentValidation *bool
1517
Comment interface{}
18+
19+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
20+
// release.
21+
Internal optionsutil.Options
1622
}
1723

1824
// InsertOneOptionsBuilder represents functional options that configure an
@@ -61,6 +67,10 @@ type InsertManyOptions struct {
6167
BypassDocumentValidation *bool
6268
Comment interface{}
6369
Ordered *bool
70+
71+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
72+
// release.
73+
Internal optionsutil.Options
6474
}
6575

6676
// InsertManyOptionsBuilder contains options to configure insert operations.

x/mongo/driver/operation/aggregate.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Aggregate struct {
5050
customOptions map[string]bsoncore.Value
5151
timeout *time.Duration
5252
omitMaxTimeMS bool
53+
rawData *bool
5354

5455
result driver.CursorResponse
5556
}
@@ -159,6 +160,10 @@ func (a *Aggregate) command(dst []byte, desc description.SelectedServer) ([]byte
159160
if a.let != nil {
160161
dst = bsoncore.AppendDocumentElement(dst, "let", a.let)
161162
}
163+
// Set rawData for 8.2+ servers.
164+
if a.rawData != nil && desc.WireVersion != nil && driverutil.VersionRangeIncludes(*desc.WireVersion, 27) {
165+
dst = bsoncore.AppendBooleanElement(dst, "rawData", *a.rawData)
166+
}
162167
for optionName, optionValue := range a.customOptions {
163168
dst = bsoncore.AppendValueElement(dst, optionName, optionValue)
164169
}
@@ -431,3 +436,13 @@ func (a *Aggregate) OmitMaxTimeMS(omit bool) *Aggregate {
431436
a.omitMaxTimeMS = omit
432437
return a
433438
}
439+
440+
// RawData sets the rawData to access timeseries data in the compressed format.
441+
func (a *Aggregate) RawData(rawData bool) *Aggregate {
442+
if a == nil {
443+
a = new(Aggregate)
444+
}
445+
446+
a.rawData = &rawData
447+
return a
448+
}

0 commit comments

Comments
 (0)