Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ func (coll *Collection) insert(
if args.Ordered != nil {
op = op.Ordered(*args.Ordered)
}
if args.RawData != nil {
op = op.RawData(*args.RawData)
}
retry := driver.RetryNone
if coll.client.retryWrites {
retry = driver.RetryOncePerCommand
Expand Down Expand Up @@ -375,6 +378,9 @@ func (coll *Collection) InsertOne(ctx context.Context, document interface{},
if args.Comment != nil {
imOpts.SetComment(args.Comment)
}
if args.RawData != nil {
imOpts = imOpts.SetRawData(*args.RawData)
}
res, err := coll.insert(ctx, []interface{}{document}, imOpts)

rr, err := processWriteError(err)
Expand Down
26 changes: 26 additions & 0 deletions mongo/options/insertoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package options
type InsertOneOptions struct {
BypassDocumentValidation *bool
Comment interface{}
RawData *bool
}

// InsertOneOptionsBuilder represents functional options that configure an
Expand Down Expand Up @@ -53,6 +54,18 @@ func (ioo *InsertOneOptionsBuilder) SetComment(comment interface{}) *InsertOneOp
return ioo
}

// SetRawData sets the value for the RawData field. If true, it allows the CRUD operations to access timeseries
// collections on the bucket-level. This option is only valid for MongoDB versions >= 9.0. The default value is false.
func (ioo *InsertOneOptionsBuilder) SetRawData(rawData bool) *InsertOneOptionsBuilder {
Copy link
Member

@prestonvasquez prestonvasquez Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the scope:

Customers do not need to be able to set this in their applications. The interface will be marked “for internal use only”.

It sounds like we should add this as a deprecated option to InsertOneOptions and add a SetInternalInsertOneOptions function to the xoptions package, rather than exposing an options-level setter. It sounds like nobody will use this but internal teams, we can pivot in the future if necessary.

opts := options.InsertOne()
_ = xoptions.SetInternalInsertOneOptions(opts, "rawData", true)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the rawData option should be internal only. However, I doubt if it should be a client level option because it makes users to set up multiple clients to handle different scenario. IMO, for operation options following the Lister interface with a correlated "optionsBuilder" type, we can have a setter, for example:

// Deprecated: This function is for internal use only. It may be changed or removed in any release.
func (ao *AggregateOptionsBuilder) SetInternalOption(key string, option any) error

What are your thoughts?
cc: @matthewdale

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see an internal-only options pattern was added a few months ago: xoptions.SetInternalClientOptions. Can we extend that pattern for this use case?

Copy link
Collaborator Author

@qingyang-hu qingyang-hu Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing pattern for SetInternalClientOptions is meant for ClientOptions. I think that adding rawData directly as a client option will cause inconvenience in most use cases, such as when setting an option for a specific operation.

Another option would be like xoptions.SetInternalAggregateOptions(opts *AggregateOptionsBuilder, key string, option any) error. We add a setter method for each OptionsBuilder in the xoptions package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qingyang-hu This sounds correct:

We add a setter method for each OptionsBuilder in the xoptions package.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like there are two good options based on the existing pattern of SetInternalClientOptions. I prefer the first one because it can be extended to support future internal options without any additional API changes. Does that sound reasonable?

Custom key/value pairs with xoptions setter

Add a Custom optionsutil.Options field to every CRUD method options struct (like ClientOptions.Custom). Add a function to xoptions to set custom k/v pairs in Custom.

E.g.

package options

type FindOptions struct {
    // ...
    Custom optionsutil.Options
}
package xoptions

func SetFindOneCustom(f *options.FindOneOptionsBuilder, key string, option any) {
	f.Opts = append(f.Opts, func(opts *FindOneOptions) error {
		opts.Custom = optionsutil.WithValue(opts.Custom, key, option)
		return nil
	})
}

RawData bool option with xoptions setter

Add a RawData *bool field to every CRUD method options struct. Add a function to xoptions to set RawData for each supported option type.

E.g.

package options

type FindOptions struct {
    // ...
    RawData *bool
}
package xoptions

func SetFindOneRawData(f *options.FindOneOptionsBuilder, b bool) {
	f.Opts = append(f.Opts, func(opts *FindOneOptions) error {
		opts.RawData = &b
		return nil
	})
}

ioo.Opts = append(ioo.Opts, func(ioo *InsertOneOptions) error {
ioo.RawData = &rawData

return nil
})

return ioo
}

// InsertManyOptions represents arguments that can be used to configure an
// InsertMany operation.
//
Expand All @@ -61,6 +74,7 @@ type InsertManyOptions struct {
BypassDocumentValidation *bool
Comment interface{}
Ordered *bool
RawData *bool
}

// InsertManyOptionsBuilder contains options to configure insert operations.
Expand Down Expand Up @@ -121,3 +135,15 @@ func (imo *InsertManyOptionsBuilder) SetOrdered(b bool) *InsertManyOptionsBuilde

return imo
}

// SetRawData sets the value for the RawData field. If true, it allows the CRUD operations to access timeseries
// collections on the bucket-level. This option is only valid for MongoDB versions >= 9.0. The default value is false.
func (imo *InsertManyOptionsBuilder) SetRawData(rawData bool) *InsertManyOptionsBuilder {
imo.Opts = append(imo.Opts, func(opts *InsertManyOptions) error {
opts.RawData = &rawData

return nil
})

return imo
}
15 changes: 15 additions & 0 deletions x/mongo/driver/operation/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Insert struct {
result InsertResult
serverAPI *driver.ServerAPIOptions
timeout *time.Duration
rawData *bool
logger *logger.Logger
}

Expand Down Expand Up @@ -132,6 +133,10 @@ func (i *Insert) command(dst []byte, desc description.SelectedServer) ([]byte, e
if i.ordered != nil {
dst = bsoncore.AppendBooleanElement(dst, "ordered", *i.ordered)
}
// Set rawData for 8.2+ servers.
if i.rawData != nil && desc.WireVersion != nil && driverutil.VersionRangeIncludes(*desc.WireVersion, 27) {
dst = bsoncore.AppendBooleanElement(dst, "rawData", *i.rawData)
}
return dst, nil
}

Expand Down Expand Up @@ -318,3 +323,13 @@ func (i *Insert) Authenticator(authenticator driver.Authenticator) *Insert {
i.authenticator = authenticator
return i
}

// RawData sets the rawData to access timeseries data in the compressed format.
func (i *Insert) RawData(rawData bool) *Insert {
if i == nil {
i = new(Insert)
}

i.rawData = &rawData
return i
}
Loading