/
bulk_write_models.go
245 lines (205 loc) · 8.58 KB
/
bulk_write_models.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
// 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 mongo
import (
"go.mongodb.org/mongo-driver/mongo/options"
)
// WriteModel is an interface implemented by models that can be used in a BulkWrite operation. Each WriteModel
// represents a write.
//
// This interface is implemented by InsertOneModel, DeleteOneModel, DeleteManyModel, ReplaceOneModel, UpdateOneModel,
// and UpdateManyModel. Custom implementations of this interface must not be used.
type WriteModel interface {
writeModel()
}
// InsertOneModel is used to insert a single document in a BulkWrite operation.
type InsertOneModel struct {
Document interface{}
}
// NewInsertOneModel creates a new InsertOneModel.
func NewInsertOneModel() *InsertOneModel {
return &InsertOneModel{}
}
// SetDocument specifies the document to be inserted. The document cannot be nil. If it does not have an _id field when
// transformed into BSON, one will be added automatically to the marshalled document. The original document will not be
// modified.
func (iom *InsertOneModel) SetDocument(doc interface{}) *InsertOneModel {
iom.Document = doc
return iom
}
func (*InsertOneModel) writeModel() {}
// DeleteOneModel is used to delete at most one document in a BulkWriteOperation.
type DeleteOneModel struct {
Filter interface{}
Collation *options.Collation
}
// NewDeleteOneModel creates a new DeleteOneModel.
func NewDeleteOneModel() *DeleteOneModel {
return &DeleteOneModel{}
}
// SetFilter specifies a filter to use to select the document to delete. The filter must be a document containing query
// operators. It cannot be nil. If the filter matches multiple documents, one will be selected from the matching
// documents.
func (dom *DeleteOneModel) SetFilter(filter interface{}) *DeleteOneModel {
dom.Filter = filter
return dom
}
// SetCollation specifies a collation to use for string comparisons. The default is nil, meaning no collation will be
// used.
func (dom *DeleteOneModel) SetCollation(collation *options.Collation) *DeleteOneModel {
dom.Collation = collation
return dom
}
func (*DeleteOneModel) writeModel() {}
// DeleteManyModel is used to delete multiple documents in a BulkWrite operation.
type DeleteManyModel struct {
Filter interface{}
Collation *options.Collation
}
// NewDeleteManyModel creates a new DeleteManyModel.
func NewDeleteManyModel() *DeleteManyModel {
return &DeleteManyModel{}
}
// SetFilter specifies a filter to use to select documents to delete. The filter must be a document containing query
// operators. It cannot be nil.
func (dmm *DeleteManyModel) SetFilter(filter interface{}) *DeleteManyModel {
dmm.Filter = filter
return dmm
}
// SetCollation specifies a collation to use for string comparisons. The default is nil, meaning no collation will be
// used.
func (dmm *DeleteManyModel) SetCollation(collation *options.Collation) *DeleteManyModel {
dmm.Collation = collation
return dmm
}
func (*DeleteManyModel) writeModel() {}
// ReplaceOneModel is used to replace at most one document in a BulkWrite operation.
type ReplaceOneModel struct {
Collation *options.Collation
Upsert *bool
Filter interface{}
Replacement interface{}
}
// NewReplaceOneModel creates a new ReplaceOneModel.
func NewReplaceOneModel() *ReplaceOneModel {
return &ReplaceOneModel{}
}
// SetFilter specifies a filter to use to select the document to replace. The filter must be a document containing query
// operators. It cannot be nil. If the filter matches multiple documents, one will be selected from the matching
// documents.
func (rom *ReplaceOneModel) SetFilter(filter interface{}) *ReplaceOneModel {
rom.Filter = filter
return rom
}
// SetReplacement specifies a document that will be used to replace the selected document. It cannot be nil and cannot
// contain any update operators (https://docs.mongodb.com/manual/reference/operator/update/).
func (rom *ReplaceOneModel) SetReplacement(rep interface{}) *ReplaceOneModel {
rom.Replacement = rep
return rom
}
// SetCollation specifies a collation to use for string comparisons. The default is nil, meaning no collation will be
// used.
func (rom *ReplaceOneModel) SetCollation(collation *options.Collation) *ReplaceOneModel {
rom.Collation = collation
return rom
}
// SetUpsert specifies whether or not the replacement document should be inserted if no document matching the filter is
// found. If an upsert is performed, the _id of the upserted document can be retrieved from the UpsertedIDs field of the
// BulkWriteResult.
func (rom *ReplaceOneModel) SetUpsert(upsert bool) *ReplaceOneModel {
rom.Upsert = &upsert
return rom
}
func (*ReplaceOneModel) writeModel() {}
// UpdateOneModel is used to update at most one document in a BulkWrite operation.
type UpdateOneModel struct {
Collation *options.Collation
Upsert *bool
Filter interface{}
Update interface{}
ArrayFilters *options.ArrayFilters
}
// NewUpdateOneModel creates a new UpdateOneModel.
func NewUpdateOneModel() *UpdateOneModel {
return &UpdateOneModel{}
}
// SetFilter specifies a filter to use to select the document to update. The filter must be a document containing query
// operators. It cannot be nil. If the filter matches multiple documents, one will be selected from the matching
// documents.
func (uom *UpdateOneModel) SetFilter(filter interface{}) *UpdateOneModel {
uom.Filter = filter
return uom
}
// SetUpdate specifies the modifications to be made to the selected document. The value must be a document containing
// update operators (https://docs.mongodb.com/manual/reference/operator/update/). It cannot be nil or empty.
func (uom *UpdateOneModel) SetUpdate(update interface{}) *UpdateOneModel {
uom.Update = update
return uom
}
// SetArrayFilters specifies a set of filters to determine which elements should be modified when updating an array
// field.
func (uom *UpdateOneModel) SetArrayFilters(filters options.ArrayFilters) *UpdateOneModel {
uom.ArrayFilters = &filters
return uom
}
// SetCollation specifies a collation to use for string comparisons. The default is nil, meaning no collation will be
// used.
func (uom *UpdateOneModel) SetCollation(collation *options.Collation) *UpdateOneModel {
uom.Collation = collation
return uom
}
// SetUpsert specifies whether or not a new document should be inserted if no document matching the filter is found. If
// an upsert is performed, the _id of the upserted document can be retrieved from the UpsertedIDs field of the
// BulkWriteResult.
func (uom *UpdateOneModel) SetUpsert(upsert bool) *UpdateOneModel {
uom.Upsert = &upsert
return uom
}
func (*UpdateOneModel) writeModel() {}
// UpdateManyModel is used to update multiple documents in a BulkWrite operation.
type UpdateManyModel struct {
Collation *options.Collation
Upsert *bool
Filter interface{}
Update interface{}
ArrayFilters *options.ArrayFilters
}
// NewUpdateManyModel creates a new UpdateManyModel.
func NewUpdateManyModel() *UpdateManyModel {
return &UpdateManyModel{}
}
// SetFilter specifies a filter to use to select documents to update. The filter must be a document containing query
// operators. It cannot be nil.
func (umm *UpdateManyModel) SetFilter(filter interface{}) *UpdateManyModel {
umm.Filter = filter
return umm
}
// SetUpdate specifies the modifications to be made to the selected documents. The value must be a document containing
// update operators (https://docs.mongodb.com/manual/reference/operator/update/). It cannot be nil or empty.
func (umm *UpdateManyModel) SetUpdate(update interface{}) *UpdateManyModel {
umm.Update = update
return umm
}
// SetArrayFilters specifies a set of filters to determine which elements should be modified when updating an array
// field.
func (umm *UpdateManyModel) SetArrayFilters(filters options.ArrayFilters) *UpdateManyModel {
umm.ArrayFilters = &filters
return umm
}
// SetCollation specifies a collation to use for string comparisons. The default is nil, meaning no collation will be
// used.
func (umm *UpdateManyModel) SetCollation(collation *options.Collation) *UpdateManyModel {
umm.Collation = collation
return umm
}
// SetUpsert specifies whether or not a new document should be inserted if no document matching the filter is found. If
// an upsert is performed, the _id of the upserted document can be retrieved from the UpsertedIDs field of the
// BulkWriteResult.
func (umm *UpdateManyModel) SetUpsert(upsert bool) *UpdateManyModel {
umm.Upsert = &upsert
return umm
}
func (*UpdateManyModel) writeModel() {}