Skip to content

Commit 4bdff2e

Browse files
author
Divjot Arora
authored
GODRIVER-1329 Document results, errors, and BulkWrite models (#230)
1 parent 4aec8e2 commit 4bdff2e

File tree

5 files changed

+149
-103
lines changed

5 files changed

+149
-103
lines changed

mongo/bulk_write_models.go

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import (
1010
"go.mongodb.org/mongo-driver/mongo/options"
1111
)
1212

13-
// WriteModel is the interface satisfied by all models for bulk writes.
13+
// WriteModel is an interface implemented by models that can be used in a BulkWrite operation. Each WriteModel
14+
// represents a write.
15+
//
16+
// This interface is implemented by InsertOneModel, DeleteOneModel, DeleteManyModel, ReplaceOneModel, UpdateOneModel,
17+
// and UpdateManyModel. Custom implementations of this interface must not be used.
1418
type WriteModel interface {
1519
writeModel()
1620
}
1721

18-
// InsertOneModel is the write model for insert operations.
22+
// InsertOneModel is used to insert a single document in a BulkWrite operation.
1923
type InsertOneModel struct {
2024
Document interface{}
2125
}
@@ -25,15 +29,17 @@ func NewInsertOneModel() *InsertOneModel {
2529
return &InsertOneModel{}
2630
}
2731

28-
// SetDocument sets the BSON document for the InsertOneModel.
32+
// SetDocument specifies the document to be inserted. The document cannot be nil. If it does not have an _id field when
33+
// transformed into BSON, one will be added automatically to the marshalled document. The original document will not be
34+
// modified.
2935
func (iom *InsertOneModel) SetDocument(doc interface{}) *InsertOneModel {
3036
iom.Document = doc
3137
return iom
3238
}
3339

3440
func (*InsertOneModel) writeModel() {}
3541

36-
// DeleteOneModel is the write model for delete operations.
42+
// DeleteOneModel is used to delete at most one document in a BulkWriteOperation.
3743
type DeleteOneModel struct {
3844
Filter interface{}
3945
Collation *options.Collation
@@ -44,21 +50,24 @@ func NewDeleteOneModel() *DeleteOneModel {
4450
return &DeleteOneModel{}
4551
}
4652

47-
// SetFilter sets the filter for the DeleteOneModel.
53+
// SetFilter specifies a filter to use to select the document to delete. The filter must be a document containing query
54+
// operators. It cannot be nil. If the filter matches multiple documents, one will be selected from the matching
55+
// documents.
4856
func (dom *DeleteOneModel) SetFilter(filter interface{}) *DeleteOneModel {
4957
dom.Filter = filter
5058
return dom
5159
}
5260

53-
// SetCollation sets the collation for the DeleteOneModel.
61+
// SetCollation specifies a collation to use for string comparisons. The default is nil, meaning no collation will be
62+
// used.
5463
func (dom *DeleteOneModel) SetCollation(collation *options.Collation) *DeleteOneModel {
5564
dom.Collation = collation
5665
return dom
5766
}
5867

5968
func (*DeleteOneModel) writeModel() {}
6069

61-
// DeleteManyModel is the write model for deleteMany operations.
70+
// DeleteManyModel is used to delete multiple documents in a BulkWrite operation.
6271
type DeleteManyModel struct {
6372
Filter interface{}
6473
Collation *options.Collation
@@ -69,21 +78,23 @@ func NewDeleteManyModel() *DeleteManyModel {
6978
return &DeleteManyModel{}
7079
}
7180

72-
// SetFilter sets the filter for the DeleteManyModel.
81+
// SetFilter specifies a filter to use to select documents to delete. The filter must be a document containing query
82+
// operators. It cannot be nil.
7383
func (dmm *DeleteManyModel) SetFilter(filter interface{}) *DeleteManyModel {
7484
dmm.Filter = filter
7585
return dmm
7686
}
7787

78-
// SetCollation sets the collation for the DeleteManyModel.
88+
// SetCollation specifies a collation to use for string comparisons. The default is nil, meaning no collation will be
89+
// used.
7990
func (dmm *DeleteManyModel) SetCollation(collation *options.Collation) *DeleteManyModel {
8091
dmm.Collation = collation
8192
return dmm
8293
}
8394

8495
func (*DeleteManyModel) writeModel() {}
8596

86-
// ReplaceOneModel is the write model for replace operations.
97+
// ReplaceOneModel is used to replace at most one document in a BulkWrite operation.
8798
type ReplaceOneModel struct {
8899
Collation *options.Collation
89100
Upsert *bool
@@ -96,33 +107,39 @@ func NewReplaceOneModel() *ReplaceOneModel {
96107
return &ReplaceOneModel{}
97108
}
98109

99-
// SetFilter sets the filter for the ReplaceOneModel.
110+
// SetFilter specifies a filter to use to select the document to replace. The filter must be a document containing query
111+
// operators. It cannot be nil. If the filter matches multiple documents, one will be selected from the matching
112+
// documents.
100113
func (rom *ReplaceOneModel) SetFilter(filter interface{}) *ReplaceOneModel {
101114
rom.Filter = filter
102115
return rom
103116
}
104117

105-
// SetReplacement sets the replacement document for the ReplaceOneModel.
118+
// SetReplacement specifies a document that will be used to replace the selected document. It cannot be nil and cannot
119+
// contain any update operators (https://docs.mongodb.com/manual/reference/operator/update/).
106120
func (rom *ReplaceOneModel) SetReplacement(rep interface{}) *ReplaceOneModel {
107121
rom.Replacement = rep
108122
return rom
109123
}
110124

111-
// SetCollation sets the collation for the ReplaceOneModel.
125+
// SetCollation specifies a collation to use for string comparisons. The default is nil, meaning no collation will be
126+
// used.
112127
func (rom *ReplaceOneModel) SetCollation(collation *options.Collation) *ReplaceOneModel {
113128
rom.Collation = collation
114129
return rom
115130
}
116131

117-
// SetUpsert specifies if a new document should be created if no document matches the query.
132+
// SetUpsert specifies whether or not the replacement document should be inserted if no document matching the filter is
133+
// found. If an upsert is performed, the _id of the upserted document can be retrieved from the UpsertedIDs field of the
134+
// BulkWriteResult.
118135
func (rom *ReplaceOneModel) SetUpsert(upsert bool) *ReplaceOneModel {
119136
rom.Upsert = &upsert
120137
return rom
121138
}
122139

123140
func (*ReplaceOneModel) writeModel() {}
124141

125-
// UpdateOneModel is the write model for update operations.
142+
// UpdateOneModel is used to update at most one document in a BulkWrite operation.
126143
type UpdateOneModel struct {
127144
Collation *options.Collation
128145
Upsert *bool
@@ -136,39 +153,46 @@ func NewUpdateOneModel() *UpdateOneModel {
136153
return &UpdateOneModel{}
137154
}
138155

139-
// SetFilter sets the filter for the UpdateOneModel.
156+
// SetFilter specifies a filter to use to select the document to update. The filter must be a document containing query
157+
// operators. It cannot be nil. If the filter matches multiple documents, one will be selected from the matching
158+
// documents.
140159
func (uom *UpdateOneModel) SetFilter(filter interface{}) *UpdateOneModel {
141160
uom.Filter = filter
142161
return uom
143162
}
144163

145-
// SetUpdate sets the update document for the UpdateOneModel.
164+
// SetUpdate specifies the modifications to be made to the selected document. The value must be a document containing
165+
// update operators (https://docs.mongodb.com/manual/reference/operator/update/). It cannot be nil or empty.
146166
func (uom *UpdateOneModel) SetUpdate(update interface{}) *UpdateOneModel {
147167
uom.Update = update
148168
return uom
149169
}
150170

151-
// SetArrayFilters specifies a set of filters specifying to which array elements an update should apply.
171+
// SetArrayFilters specifies a set of filters to determine which elements should be modified when updating an array
172+
// field.
152173
func (uom *UpdateOneModel) SetArrayFilters(filters options.ArrayFilters) *UpdateOneModel {
153174
uom.ArrayFilters = &filters
154175
return uom
155176
}
156177

157-
// SetCollation sets the collation for the UpdateOneModel.
178+
// SetCollation specifies a collation to use for string comparisons. The default is nil, meaning no collation will be
179+
// used.
158180
func (uom *UpdateOneModel) SetCollation(collation *options.Collation) *UpdateOneModel {
159181
uom.Collation = collation
160182
return uom
161183
}
162184

163-
// SetUpsert specifies if a new document should be created if no document matches the query.
185+
// SetUpsert specifies whether or not a new document should be inserted if no document matching the filter is found. If
186+
// an upsert is performed, the _id of the upserted document can be retrieved from the UpsertedIDs field of the
187+
// BulkWriteResult.
164188
func (uom *UpdateOneModel) SetUpsert(upsert bool) *UpdateOneModel {
165189
uom.Upsert = &upsert
166190
return uom
167191
}
168192

169193
func (*UpdateOneModel) writeModel() {}
170194

171-
// UpdateManyModel is the write model for updateMany operations.
195+
// UpdateManyModel is used to update multiple documents in a BulkWrite operation.
172196
type UpdateManyModel struct {
173197
Collation *options.Collation
174198
Upsert *bool
@@ -182,31 +206,37 @@ func NewUpdateManyModel() *UpdateManyModel {
182206
return &UpdateManyModel{}
183207
}
184208

185-
// SetFilter sets the filter for the UpdateManyModel.
209+
// SetFilter specifies a filter to use to select documents to update. The filter must be a document containing query
210+
// operators. It cannot be nil.
186211
func (umm *UpdateManyModel) SetFilter(filter interface{}) *UpdateManyModel {
187212
umm.Filter = filter
188213
return umm
189214
}
190215

191-
// SetUpdate sets the update document for the UpdateManyModel.
216+
// SetUpdate specifies the modifications to be made to the selected documents. The value must be a document containing
217+
// update operators (https://docs.mongodb.com/manual/reference/operator/update/). It cannot be nil or empty.
192218
func (umm *UpdateManyModel) SetUpdate(update interface{}) *UpdateManyModel {
193219
umm.Update = update
194220
return umm
195221
}
196222

197-
// SetArrayFilters specifies a set of filters specifying to which array elements an update should apply.
223+
// SetArrayFilters specifies a set of filters to determine which elements should be modified when updating an array
224+
// field.
198225
func (umm *UpdateManyModel) SetArrayFilters(filters options.ArrayFilters) *UpdateManyModel {
199226
umm.ArrayFilters = &filters
200227
return umm
201228
}
202229

203-
// SetCollation sets the collation for the UpdateManyModel.
230+
// SetCollation specifies a collation to use for string comparisons. The default is nil, meaning no collation will be
231+
// used.
204232
func (umm *UpdateManyModel) SetCollation(collation *options.Collation) *UpdateManyModel {
205233
umm.Collation = collation
206234
return umm
207235
}
208236

209-
// SetUpsert specifies if a new document should be created if no document matches the query.
237+
// SetUpsert specifies whether or not a new document should be inserted if no document matching the filter is found. If
238+
// an upsert is performed, the _id of the upserted document can be retrieved from the UpsertedIDs field of the
239+
// BulkWriteResult.
210240
func (umm *UpdateManyModel) SetUpsert(upsert bool) *UpdateManyModel {
211241
umm.Upsert = &upsert
212242
return umm

mongo/collection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ func (coll *Collection) delete(ctx context.Context, filter interface{}, deleteOn
443443

444444
// DeleteOne executes a delete command to delete at most one document from the collection.
445445
//
446-
// The filter parameter must be a document containing query parameters and can be used to select the document to be
446+
// The filter parameter must be a document containing query operators and can be used to select the document to be
447447
// deleted. It cannot be nil. If the filter does not match any documents, the operation will succeed and a DeleteResult
448448
// with a DeletedCount of 0 will be returned. If the filter matches multiple documents, one will be selected from the
449449
// matched set.
@@ -459,7 +459,7 @@ func (coll *Collection) DeleteOne(ctx context.Context, filter interface{},
459459

460460
// DeleteMany executes a delete command to delete documents from the collection.
461461
//
462-
// The filter parameter must be a document containing query parameters and can be used to select the documents to
462+
// The filter parameter must be a document containing query operators and can be used to select the documents to
463463
// be deleted. It cannot be nil. An empty document (e.g. bson.D{}) should be used to delete all documents in the
464464
// collection. If the filter does not match any documents, the operation will succeed and a DeleteResult with a
465465
// DeletedCount of 0 will be returned.

0 commit comments

Comments
 (0)