-
Notifications
You must be signed in to change notification settings - Fork 883
/
createcollectionoptions.go
360 lines (302 loc) · 13.4 KB
/
createcollectionoptions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package options
import "time"
// DefaultIndexOptions represents the default options for a collection to apply on new indexes. This type can be used
// when creating a new collection through the CreateCollectionOptions.SetDefaultIndexOptions method.
type DefaultIndexOptions struct {
// Specifies the storage engine to use for the index. The value must be a document in the form
// {<storage engine name>: <options>}. The default value is nil, which means that the default storage engine
// will be used.
StorageEngine interface{}
}
// DefaultIndex creates a new DefaultIndexOptions instance.
func DefaultIndex() *DefaultIndexOptions {
return &DefaultIndexOptions{}
}
// SetStorageEngine sets the value for the StorageEngine field.
func (d *DefaultIndexOptions) SetStorageEngine(storageEngine interface{}) *DefaultIndexOptions {
d.StorageEngine = storageEngine
return d
}
// TimeSeriesOptions specifies options on a time-series collection.
type TimeSeriesOptions struct {
// TimeField is the top-level field to be used for time. Inserted documents must have this field,
// and the field must be of the BSON UTC datetime type (0x9).
TimeField string
// MetaField is the name of the top-level field describing the series. This field is used to group
// related data and may be of any BSON type, except for array. This name may not be the same
// as the TimeField or _id. This field is optional.
MetaField *string
// Granularity is the granularity of time-series data. Allowed granularity options are
// "seconds", "minutes" and "hours". This field is optional.
Granularity *string
// BucketMaxSpan is the maximum range of time values for a bucket. The
// time.Duration is rounded down to the nearest second and applied as
// the command option: "bucketRoundingSeconds". This field is optional.
BucketMaxSpan *time.Duration
// BucketRounding is used to determine the minimum time boundary when
// opening a new bucket by rounding the first timestamp down to the next
// multiple of this value. The time.Duration is rounded down to the
// nearest second and applied as the command option:
// "bucketRoundingSeconds". This field is optional.
BucketRounding *time.Duration
}
// TimeSeries creates a new TimeSeriesOptions instance.
func TimeSeries() *TimeSeriesOptions {
return &TimeSeriesOptions{}
}
// SetTimeField sets the value for the TimeField.
func (tso *TimeSeriesOptions) SetTimeField(timeField string) *TimeSeriesOptions {
tso.TimeField = timeField
return tso
}
// SetMetaField sets the value for the MetaField.
func (tso *TimeSeriesOptions) SetMetaField(metaField string) *TimeSeriesOptions {
tso.MetaField = &metaField
return tso
}
// SetGranularity sets the value for Granularity.
func (tso *TimeSeriesOptions) SetGranularity(granularity string) *TimeSeriesOptions {
tso.Granularity = &granularity
return tso
}
// SetBucketMaxSpan sets the value for BucketMaxSpan.
func (tso *TimeSeriesOptions) SetBucketMaxSpan(dur time.Duration) *TimeSeriesOptions {
tso.BucketMaxSpan = &dur
return tso
}
// SetBucketRounding sets the value for BucketRounding.
func (tso *TimeSeriesOptions) SetBucketRounding(dur time.Duration) *TimeSeriesOptions {
tso.BucketRounding = &dur
return tso
}
// CreateCollectionOptions represents options that can be used to configure a CreateCollection operation.
type CreateCollectionOptions struct {
// Specifies if the collection is capped (see https://www.mongodb.com/docs/manual/core/capped-collections/). If true,
// the SizeInBytes option must also be specified. The default value is false.
Capped *bool
// Specifies the default collation for the new collection. This option is only valid for MongoDB versions >= 3.4.
// For previous server versions, the driver will return an error if this option is used. The default value is nil.
Collation *Collation
// Specifies how change streams opened against the collection can return pre- and post-images of updated
// documents. The value must be a document in the form {<option name>: <options>}. This option is only valid for
// MongoDB versions >= 6.0. The default value is nil, which means that change streams opened against the collection
// will not return pre- and post-images of updated documents in any way.
ChangeStreamPreAndPostImages interface{}
// Specifies a default configuration for indexes on the collection. This option is only valid for MongoDB versions
// >= 3.4. The default value is nil, meaning indexes will be configured using server defaults.
DefaultIndexOptions *DefaultIndexOptions
// Specifies the maximum number of documents allowed in a capped collection. The limit specified by the SizeInBytes
// option takes precedence over this option. If a capped collection reaches its size limit, old documents will be
// removed, regardless of the number of documents in the collection. The default value is 0, meaning the maximum
// number of documents is unbounded.
MaxDocuments *int64
// Specifies the maximum size in bytes for a capped collection. The default value is 0.
SizeInBytes *int64
// Specifies the storage engine to use for the index. The value must be a document in the form
// {<storage engine name>: <options>}. The default value is nil, which means that the default storage engine
// will be used.
StorageEngine interface{}
// Specifies what should happen if a document being inserted does not pass validation. Valid values are "error" and
// "warn". See https://www.mongodb.com/docs/manual/core/schema-validation/#accept-or-reject-invalid-documents for more
// information. This option is only valid for MongoDB versions >= 3.2. The default value is "error".
ValidationAction *string
// Specifies how strictly the server applies validation rules to existing documents in the collection during update
// operations. Valid values are "off", "strict", and "moderate". See
// https://www.mongodb.com/docs/manual/core/schema-validation/#existing-documents for more information. This option is
// only valid for MongoDB versions >= 3.2. The default value is "strict".
ValidationLevel *string
// A document specifying validation rules for the collection. See
// https://www.mongodb.com/docs/manual/core/schema-validation/ for more information about schema validation. This option
// is only valid for MongoDB versions >= 3.2. The default value is nil, meaning no validator will be used for the
// collection.
Validator interface{}
// Value indicating after how many seconds old time-series data should be deleted. See
// https://www.mongodb.com/docs/manual/reference/command/create/ for supported options, and
// https://www.mongodb.com/docs/manual/core/timeseries-collections/ for more information on time-series
// collections.
//
// This option is only valid for MongoDB versions >= 5.0
ExpireAfterSeconds *int64
// Options for specifying a time-series collection. See
// https://www.mongodb.com/docs/manual/reference/command/create/ for supported options, and
// https://www.mongodb.com/docs/manual/core/timeseries-collections/ for more information on time-series
// collections.
//
// This option is only valid for MongoDB versions >= 5.0
TimeSeriesOptions *TimeSeriesOptions
// EncryptedFields configures encrypted fields.
//
// This option is only valid for MongoDB versions >= 6.0
EncryptedFields interface{}
// ClusteredIndex is used to create a collection with a clustered index.
//
// This option is only valid for MongoDB versions >= 5.3
ClusteredIndex interface{}
}
// CreateCollection creates a new CreateCollectionOptions instance.
func CreateCollection() *CreateCollectionOptions {
return &CreateCollectionOptions{}
}
// SetCapped sets the value for the Capped field.
func (c *CreateCollectionOptions) SetCapped(capped bool) *CreateCollectionOptions {
c.Capped = &capped
return c
}
// SetCollation sets the value for the Collation field.
func (c *CreateCollectionOptions) SetCollation(collation *Collation) *CreateCollectionOptions {
c.Collation = collation
return c
}
// SetChangeStreamPreAndPostImages sets the value for the ChangeStreamPreAndPostImages field.
func (c *CreateCollectionOptions) SetChangeStreamPreAndPostImages(csppi interface{}) *CreateCollectionOptions {
c.ChangeStreamPreAndPostImages = &csppi
return c
}
// SetDefaultIndexOptions sets the value for the DefaultIndexOptions field.
func (c *CreateCollectionOptions) SetDefaultIndexOptions(opts *DefaultIndexOptions) *CreateCollectionOptions {
c.DefaultIndexOptions = opts
return c
}
// SetMaxDocuments sets the value for the MaxDocuments field.
func (c *CreateCollectionOptions) SetMaxDocuments(max int64) *CreateCollectionOptions {
c.MaxDocuments = &max
return c
}
// SetSizeInBytes sets the value for the SizeInBytes field.
func (c *CreateCollectionOptions) SetSizeInBytes(size int64) *CreateCollectionOptions {
c.SizeInBytes = &size
return c
}
// SetStorageEngine sets the value for the StorageEngine field.
func (c *CreateCollectionOptions) SetStorageEngine(storageEngine interface{}) *CreateCollectionOptions {
c.StorageEngine = &storageEngine
return c
}
// SetValidationAction sets the value for the ValidationAction field.
func (c *CreateCollectionOptions) SetValidationAction(action string) *CreateCollectionOptions {
c.ValidationAction = &action
return c
}
// SetValidationLevel sets the value for the ValidationLevel field.
func (c *CreateCollectionOptions) SetValidationLevel(level string) *CreateCollectionOptions {
c.ValidationLevel = &level
return c
}
// SetValidator sets the value for the Validator field.
func (c *CreateCollectionOptions) SetValidator(validator interface{}) *CreateCollectionOptions {
c.Validator = validator
return c
}
// SetExpireAfterSeconds sets the value for the ExpireAfterSeconds field.
func (c *CreateCollectionOptions) SetExpireAfterSeconds(eas int64) *CreateCollectionOptions {
c.ExpireAfterSeconds = &eas
return c
}
// SetTimeSeriesOptions sets the options for time-series collections.
func (c *CreateCollectionOptions) SetTimeSeriesOptions(timeSeriesOpts *TimeSeriesOptions) *CreateCollectionOptions {
c.TimeSeriesOptions = timeSeriesOpts
return c
}
// SetEncryptedFields sets the encrypted fields for encrypted collections.
func (c *CreateCollectionOptions) SetEncryptedFields(encryptedFields interface{}) *CreateCollectionOptions {
c.EncryptedFields = encryptedFields
return c
}
// SetClusteredIndex sets the value for the ClusteredIndex field.
func (c *CreateCollectionOptions) SetClusteredIndex(clusteredIndex interface{}) *CreateCollectionOptions {
c.ClusteredIndex = clusteredIndex
return c
}
// MergeCreateCollectionOptions combines the given CreateCollectionOptions instances into a single
// CreateCollectionOptions in a last-one-wins fashion.
//
// Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a
// single options struct instead.
func MergeCreateCollectionOptions(opts ...*CreateCollectionOptions) *CreateCollectionOptions {
cc := CreateCollection()
for _, opt := range opts {
if opt == nil {
continue
}
if opt.Capped != nil {
cc.Capped = opt.Capped
}
if opt.Collation != nil {
cc.Collation = opt.Collation
}
if opt.ChangeStreamPreAndPostImages != nil {
cc.ChangeStreamPreAndPostImages = opt.ChangeStreamPreAndPostImages
}
if opt.DefaultIndexOptions != nil {
cc.DefaultIndexOptions = opt.DefaultIndexOptions
}
if opt.MaxDocuments != nil {
cc.MaxDocuments = opt.MaxDocuments
}
if opt.SizeInBytes != nil {
cc.SizeInBytes = opt.SizeInBytes
}
if opt.StorageEngine != nil {
cc.StorageEngine = opt.StorageEngine
}
if opt.ValidationAction != nil {
cc.ValidationAction = opt.ValidationAction
}
if opt.ValidationLevel != nil {
cc.ValidationLevel = opt.ValidationLevel
}
if opt.Validator != nil {
cc.Validator = opt.Validator
}
if opt.ExpireAfterSeconds != nil {
cc.ExpireAfterSeconds = opt.ExpireAfterSeconds
}
if opt.TimeSeriesOptions != nil {
cc.TimeSeriesOptions = opt.TimeSeriesOptions
}
if opt.EncryptedFields != nil {
cc.EncryptedFields = opt.EncryptedFields
}
if opt.ClusteredIndex != nil {
cc.ClusteredIndex = opt.ClusteredIndex
}
}
return cc
}
// CreateViewOptions represents options that can be used to configure a CreateView operation.
type CreateViewOptions struct {
// Specifies the default collation for the new collection. This option is only valid for MongoDB versions >= 3.4.
// For previous server versions, the driver will return an error if this option is used. The default value is nil.
Collation *Collation
}
// CreateView creates an new CreateViewOptions instance.
func CreateView() *CreateViewOptions {
return &CreateViewOptions{}
}
// SetCollation sets the value for the Collation field.
func (c *CreateViewOptions) SetCollation(collation *Collation) *CreateViewOptions {
c.Collation = collation
return c
}
// MergeCreateViewOptions combines the given CreateViewOptions instances into a single CreateViewOptions in a
// last-one-wins fashion.
//
// Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a
// single options struct instead.
func MergeCreateViewOptions(opts ...*CreateViewOptions) *CreateViewOptions {
cv := CreateView()
for _, opt := range opts {
if opt == nil {
continue
}
if opt.Collation != nil {
cv.Collation = opt.Collation
}
}
return cv
}