Skip to content

Commit 6c43dc5

Browse files
authored
Add rawData option to client bulkWrite, listCollections, and index operations
1 parent b430a2e commit 6c43dc5

15 files changed

+265
-8
lines changed

internal/integration/unified/client_operation_execution.go

Lines changed: 6 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 client operations.
@@ -235,6 +236,11 @@ func executeClientBulkWrite(ctx context.Context, operation *operation) (*operati
235236
return nil, err
236237
}
237238
opts.SetWriteConcern(c)
239+
case "rawData":
240+
err = xoptions.SetInternalClientBulkWriteOptions(opts, key, val.Boolean())
241+
if err != nil {
242+
return nil, err
243+
}
238244
default:
239245
return nil, fmt.Errorf("unrecognized bulkWrite option %q", key)
240246
}

internal/integration/unified/collection_operation_execution.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ func executeCreateIndex(ctx context.Context, operation *operation) (*operationRe
241241

242242
var keys bson.Raw
243243
indexOpts := options.Index()
244+
opts := options.CreateIndexes()
244245

245246
elems, err := operation.Arguments.Elements()
246247
if err != nil {
@@ -295,6 +296,11 @@ func executeCreateIndex(ctx context.Context, operation *operation) (*operationRe
295296
indexOpts.SetWeights(val.Document())
296297
case "wildcardProjection":
297298
indexOpts.SetWildcardProjection(val.Document())
299+
case "rawData":
300+
err = xoptions.SetInternalCreateIndexesOptions(opts, key, val.Boolean())
301+
if err != nil {
302+
return nil, err
303+
}
298304
default:
299305
return nil, fmt.Errorf("unrecognized createIndex option %q", key)
300306
}
@@ -307,7 +313,8 @@ func executeCreateIndex(ctx context.Context, operation *operation) (*operationRe
307313
Keys: keys,
308314
Options: indexOpts,
309315
}
310-
name, err := coll.Indexes().CreateOne(ctx, model)
316+
317+
name, err := coll.Indexes().CreateOne(ctx, model, opts)
311318
return newValueResult(bson.TypeString, bsoncore.AppendString(nil, name), err), nil
312319
}
313320

@@ -624,6 +631,11 @@ func executeDropIndex(ctx context.Context, operation *operation) (*operationResu
624631
// ensured an analogue exists, extend "skippedTestDescriptions" to avoid
625632
// this error.
626633
return nil, fmt.Errorf("the maxTimeMS collection option is not supported")
634+
case "rawData":
635+
err = xoptions.SetInternalDropIndexesOptions(dropIndexOpts, key, val.Boolean())
636+
if err != nil {
637+
return nil, err
638+
}
627639
default:
628640
return nil, fmt.Errorf("unrecognized dropIndex option %q", key)
629641
}
@@ -1217,6 +1229,11 @@ func executeListIndexes(ctx context.Context, operation *operation) (*operationRe
12171229
switch key {
12181230
case "batchSize":
12191231
opts.SetBatchSize(val.Int32())
1232+
case "rawData":
1233+
err = xoptions.SetInternalListIndexesOptions(opts, key, val.Boolean())
1234+
if err != nil {
1235+
return nil, err
1236+
}
12201237
default:
12211238
return nil, fmt.Errorf("unrecognized listIndexes option: %q", key)
12221239
}

internal/integration/unified/crud_helpers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ func createListCollectionsArguments(args bson.Raw) (*listCollectionsArguments, e
173173
lca.filter = val.Document()
174174
case "nameOnly":
175175
lca.opts.SetNameOnly(val.Boolean())
176+
case "rawData":
177+
err := xoptions.SetInternalListCollectionsOptions(lca.opts, key, val.Boolean())
178+
if err != nil {
179+
return nil, err
180+
}
176181
default:
177182
return nil, fmt.Errorf("unrecognized listCollections option %q", key)
178183
}

mongo/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"go.mongodb.org/mongo-driver/v2/internal/httputil"
1919
"go.mongodb.org/mongo-driver/v2/internal/logger"
2020
"go.mongodb.org/mongo-driver/v2/internal/mongoutil"
21+
"go.mongodb.org/mongo-driver/v2/internal/optionsutil"
2122
"go.mongodb.org/mongo-driver/v2/internal/ptrutil"
2223
"go.mongodb.org/mongo-driver/v2/internal/serverselector"
2324
"go.mongodb.org/mongo-driver/v2/internal/uuid"
@@ -957,6 +958,11 @@ func (c *Client) BulkWrite(ctx context.Context, writes []ClientBulkWrite,
957958
selector: selector,
958959
writeConcern: wc,
959960
}
961+
if rawDataOpt := optionsutil.Value(bwo.Internal, "rawData"); rawDataOpt != nil {
962+
if rawData, ok := rawDataOpt.(bool); ok {
963+
op.rawData = &rawData
964+
}
965+
}
960966
if bwo.VerboseResults == nil || !(*bwo.VerboseResults) {
961967
op.errorsOnly = true
962968
} else if !acknowledged {

mongo/client_bulk_write.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type clientBulkWrite struct {
4444
client *Client
4545
selector description.ServerSelector
4646
writeConcern *writeconcern.WriteConcern
47+
rawData *bool
4748

4849
result ClientBulkWriteResult
4950
}
@@ -143,6 +144,10 @@ func (bw *clientBulkWrite) newCommand() func([]byte, description.SelectedServer)
143144
}
144145
dst = bsoncore.AppendDocumentElement(dst, "let", let)
145146
}
147+
// Set rawData for 8.2+ servers.
148+
if bw.rawData != nil && desc.WireVersion != nil && driverutil.VersionRangeIncludes(*desc.WireVersion, 27) {
149+
dst = bsoncore.AppendBooleanElement(dst, "rawData", *bw.rawData)
150+
}
146151
return dst, nil
147152
}
148153
}

mongo/database.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"go.mongodb.org/mongo-driver/v2/internal/csfle"
1717
"go.mongodb.org/mongo-driver/v2/internal/csot"
1818
"go.mongodb.org/mongo-driver/v2/internal/mongoutil"
19+
"go.mongodb.org/mongo-driver/v2/internal/optionsutil"
1920
"go.mongodb.org/mongo-driver/v2/internal/serverselector"
2021
"go.mongodb.org/mongo-driver/v2/mongo/options"
2122
"go.mongodb.org/mongo-driver/v2/mongo/readconcern"
@@ -487,6 +488,11 @@ func (db *Database) ListCollections(
487488
if args.AuthorizedCollections != nil {
488489
op = op.AuthorizedCollections(*args.AuthorizedCollections)
489490
}
491+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
492+
if rawData, ok := rawDataOpt.(bool); ok {
493+
op = op.RawData(rawData)
494+
}
495+
}
490496

491497
retry := driver.RetryNone
492498
if db.client.retryReads {

mongo/index_view.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strconv"
1515

1616
"go.mongodb.org/mongo-driver/v2/internal/mongoutil"
17+
"go.mongodb.org/mongo-driver/v2/internal/optionsutil"
1718
"go.mongodb.org/mongo-driver/v2/internal/serverselector"
1819
"go.mongodb.org/mongo-driver/v2/mongo/options"
1920
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
@@ -101,6 +102,11 @@ func (iv IndexView) List(ctx context.Context, opts ...options.Lister[options.Lis
101102
op = op.BatchSize(*args.BatchSize)
102103
cursorOpts.BatchSize = *args.BatchSize
103104
}
105+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
106+
if rawData, ok := rawDataOpt.(bool); ok {
107+
op = op.RawData(rawData)
108+
}
109+
}
104110

105111
retry := driver.RetryNone
106112
if iv.coll.client.retryReads {
@@ -279,6 +285,11 @@ func (iv IndexView) CreateMany(
279285

280286
op.CommitQuorum(commitQuorum)
281287
}
288+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
289+
if rawData, ok := rawDataOpt.(bool); ok {
290+
op = op.RawData(rawData)
291+
}
292+
}
282293

283294
_, err = processWriteError(op.Execute(ctx))
284295
if err != nil {
@@ -376,7 +387,12 @@ func (iv IndexView) createOptionsDoc(opts options.Lister[options.IndexOptions])
376387
return optsDoc, nil
377388
}
378389

379-
func (iv IndexView) drop(ctx context.Context, index any, _ ...options.Lister[options.DropIndexesOptions]) error {
390+
func (iv IndexView) drop(ctx context.Context, index any, opts ...options.Lister[options.DropIndexesOptions]) error {
391+
args, err := mongoutil.NewOptions[options.DropIndexesOptions](opts...)
392+
if err != nil {
393+
return fmt.Errorf("failed to construct options from builder: %w", err)
394+
}
395+
380396
if ctx == nil {
381397
ctx = context.Background()
382398
}
@@ -387,7 +403,7 @@ func (iv IndexView) drop(ctx context.Context, index any, _ ...options.Lister[opt
387403
defer sess.EndSession()
388404
}
389405

390-
err := iv.coll.client.validSession(sess)
406+
err = iv.coll.client.validSession(sess)
391407
if err != nil {
392408
return err
393409
}
@@ -408,6 +424,12 @@ func (iv IndexView) drop(ctx context.Context, index any, _ ...options.Lister[opt
408424
Deployment(iv.coll.client.deployment).ServerAPI(iv.coll.client.serverAPI).
409425
Timeout(iv.coll.client.timeout).Crypt(iv.coll.client.cryptFLE).Authenticator(iv.coll.client.authenticator)
410426

427+
if rawDataOpt := optionsutil.Value(args.Internal, "rawData"); rawDataOpt != nil {
428+
if rawData, ok := rawDataOpt.(bool); ok {
429+
op = op.RawData(rawData)
430+
}
431+
}
432+
411433
err = op.Execute(ctx)
412434
if err != nil {
413435
return wrapErrors(err)

mongo/options/clientbulkwriteoptions.go

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

99
import (
10+
"go.mongodb.org/mongo-driver/v2/internal/optionsutil"
1011
"go.mongodb.org/mongo-driver/v2/mongo/writeconcern"
1112
)
1213

@@ -20,6 +21,10 @@ type ClientBulkWriteOptions struct {
2021
Let interface{}
2122
WriteConcern *writeconcern.WriteConcern
2223
VerboseResults *bool
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
2328
}
2429

2530
// ClientBulkWriteOptionsBuilder contains options to configure client-level bulk

mongo/options/indexoptions.go

Lines changed: 15 additions & 1 deletion
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
// CreateIndexesOptions represents arguments that can be used to configure
1012
// IndexView.CreateOne and IndexView.CreateMany operations.
1113
//
1214
// See corresponding setter methods for documentation.
1315
type CreateIndexesOptions struct {
1416
CommitQuorum 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
// CreateIndexesOptionsBuilder contains options to create indexes. Each option
@@ -121,7 +127,11 @@ func (c *CreateIndexesOptionsBuilder) SetCommitQuorumVotingMembers() *CreateInde
121127

122128
// DropIndexesOptions represents arguments that can be used to configure
123129
// IndexView.DropOne and IndexView.DropAll operations.
124-
type DropIndexesOptions struct{}
130+
type DropIndexesOptions struct {
131+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
132+
// release.
133+
Internal optionsutil.Options
134+
}
125135

126136
// DropIndexesOptionsBuilder contains options to configure dropping indexes.
127137
// Each option can be set through setter functions. See documentation for each
@@ -146,6 +156,10 @@ func (d *DropIndexesOptionsBuilder) List() []func(*DropIndexesOptions) error {
146156
// See corresponding setter methods for documentation.
147157
type ListIndexesOptions struct {
148158
BatchSize *int32
159+
160+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
161+
// release.
162+
Internal optionsutil.Options
149163
}
150164

151165
// ListIndexesOptionsBuilder contains options to configure count operations. Each

mongo/options/listcollectionsoptions.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
// ListCollectionsOptions represents arguments that can be used to configure a
1012
// ListCollections operation.
1113
//
@@ -14,6 +16,10 @@ type ListCollectionsOptions struct {
1416
NameOnly *bool
1517
BatchSize *int32
1618
AuthorizedCollections *bool
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
// ListCollectionsOptionsBuilder contains options to configure list collection

0 commit comments

Comments
 (0)