Skip to content

Commit b573bb7

Browse files
committed
add insert
1 parent 4485d8e commit b573bb7

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

mongo/collection.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ func (coll *Collection) insert(
324324
if args.Ordered != nil {
325325
op = op.Ordered(*args.Ordered)
326326
}
327+
if args.RawData != nil {
328+
op = op.RawData(*args.RawData)
329+
}
327330
retry := driver.RetryNone
328331
if coll.client.retryWrites {
329332
retry = driver.RetryOncePerCommand
@@ -375,6 +378,9 @@ func (coll *Collection) InsertOne(ctx context.Context, document interface{},
375378
if args.Comment != nil {
376379
imOpts.SetComment(args.Comment)
377380
}
381+
if args.RawData != nil {
382+
imOpts = imOpts.SetRawData(*args.RawData)
383+
}
378384
res, err := coll.insert(ctx, []interface{}{document}, imOpts)
379385

380386
rr, err := processWriteError(err)

mongo/options/insertoptions.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package options
1313
type InsertOneOptions struct {
1414
BypassDocumentValidation *bool
1515
Comment interface{}
16+
RawData *bool
1617
}
1718

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

57+
// SetRawData sets the value for the RawData field. If true, it allows the CRUD operations to access timeseries
58+
// collections on the bucket-level. This option is only valid for MongoDB versions >= 9.0. The default value is false.
59+
func (ioo *InsertOneOptionsBuilder) SetRawData(rawData bool) *InsertOneOptionsBuilder {
60+
ioo.Opts = append(ioo.Opts, func(ioo *InsertOneOptions) error {
61+
ioo.RawData = &rawData
62+
63+
return nil
64+
})
65+
66+
return ioo
67+
}
68+
5669
// InsertManyOptions represents arguments that can be used to configure an
5770
// InsertMany operation.
5871
//
@@ -61,6 +74,7 @@ type InsertManyOptions struct {
6174
BypassDocumentValidation *bool
6275
Comment interface{}
6376
Ordered *bool
77+
RawData *bool
6478
}
6579

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

122136
return imo
123137
}
138+
139+
// SetRawData sets the value for the RawData field. If true, it allows the CRUD operations to access timeseries
140+
// collections on the bucket-level. This option is only valid for MongoDB versions >= 9.0. The default value is false.
141+
func (imo *InsertManyOptionsBuilder) SetRawData(rawData bool) *InsertManyOptionsBuilder {
142+
imo.Opts = append(imo.Opts, func(opts *InsertManyOptions) error {
143+
opts.RawData = &rawData
144+
145+
return nil
146+
})
147+
148+
return imo
149+
}

x/mongo/driver/operation/insert.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Insert struct {
4242
result InsertResult
4343
serverAPI *driver.ServerAPIOptions
4444
timeout *time.Duration
45+
rawData *bool
4546
logger *logger.Logger
4647
}
4748

@@ -132,6 +133,10 @@ func (i *Insert) command(dst []byte, desc description.SelectedServer) ([]byte, e
132133
if i.ordered != nil {
133134
dst = bsoncore.AppendBooleanElement(dst, "ordered", *i.ordered)
134135
}
136+
// Set rawData for 8.2+ servers.
137+
if i.rawData != nil && desc.WireVersion != nil && driverutil.VersionRangeIncludes(*desc.WireVersion, 27) {
138+
dst = bsoncore.AppendBooleanElement(dst, "rawData", *i.rawData)
139+
}
135140
return dst, nil
136141
}
137142

@@ -318,3 +323,13 @@ func (i *Insert) Authenticator(authenticator driver.Authenticator) *Insert {
318323
i.authenticator = authenticator
319324
return i
320325
}
326+
327+
// RawData sets the rawData to access timeseries data in the compressed format.
328+
func (i *Insert) RawData(rawData bool) *Insert {
329+
if i == nil {
330+
i = new(Insert)
331+
}
332+
333+
i.rawData = &rawData
334+
return i
335+
}

0 commit comments

Comments
 (0)