Skip to content

Commit 24db535

Browse files
committed
Allow sort option in client bulk write.
1 parent 5051f3b commit 24db535

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

internal/integration/unified/client_operation_execution.go

+12
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ func createClientUpdateOneModel(value bson.Raw) (*mongo.ClientBulkWrite, error)
328328
Collation *options.Collation
329329
Hint *bson.RawValue
330330
Upsert *bool
331+
Sort *bson.RawValue
331332
}
332333
err := bson.Unmarshal(value, &v)
333334
if err != nil {
@@ -340,12 +341,17 @@ func createClientUpdateOneModel(value bson.Raw) (*mongo.ClientBulkWrite, error)
340341
return nil, err
341342
}
342343
}
344+
var sort interface{}
345+
if v.Sort != nil {
346+
sort = v.Sort.Document()
347+
}
343348
model := &mongo.ClientUpdateOneModel{
344349
Filter: v.Filter,
345350
Update: v.Update,
346351
Collation: v.Collation,
347352
Hint: hint,
348353
Upsert: v.Upsert,
354+
Sort: sort,
349355
}
350356
if len(v.ArrayFilters) > 0 {
351357
model.ArrayFilters = v.ArrayFilters
@@ -405,6 +411,7 @@ func createClientReplaceOneModel(value bson.Raw) (*mongo.ClientBulkWrite, error)
405411
Collation *options.Collation
406412
Hint *bson.RawValue
407413
Upsert *bool
414+
Sort *bson.RawValue
408415
}
409416
err := bson.Unmarshal(value, &v)
410417
if err != nil {
@@ -417,6 +424,10 @@ func createClientReplaceOneModel(value bson.Raw) (*mongo.ClientBulkWrite, error)
417424
return nil, err
418425
}
419426
}
427+
var sort interface{}
428+
if v.Sort != nil {
429+
sort = v.Sort.Document()
430+
}
420431
ns := strings.SplitN(v.Namespace, ".", 2)
421432
return &mongo.ClientBulkWrite{
422433
Database: ns[0],
@@ -427,6 +438,7 @@ func createClientReplaceOneModel(value bson.Raw) (*mongo.ClientBulkWrite, error)
427438
Collation: v.Collation,
428439
Hint: hint,
429440
Upsert: v.Upsert,
441+
Sort: sort,
430442
},
431443
}, nil
432444
}

mongo/client_bulk_write.go

+14
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ func (mb *modelBatches) appendBatches(fn functionSet, dst []byte, maxCount, tota
315315
arrayFilters: model.ArrayFilters,
316316
collation: model.Collation,
317317
upsert: model.Upsert,
318+
sort: model.Sort,
318319
multi: false,
319320
checkDollarKey: true,
320321
}).marshal(mb.client.bsonOpts, mb.client.registry)
@@ -342,6 +343,7 @@ func (mb *modelBatches) appendBatches(fn functionSet, dst []byte, maxCount, tota
342343
arrayFilters: nil,
343344
collation: model.Collation,
344345
upsert: model.Upsert,
346+
sort: model.Sort,
345347
multi: false,
346348
checkDollarKey: false,
347349
}).marshal(mb.client.bsonOpts, mb.client.registry)
@@ -603,6 +605,7 @@ type clientUpdateDoc struct {
603605
hint interface{}
604606
arrayFilters []interface{}
605607
collation *options.Collation
608+
sort interface{}
606609
upsert *bool
607610
multi bool
608611
checkDollarKey bool
@@ -657,6 +660,17 @@ func (d *clientUpdateDoc) marshal(bsonOpts *options.BSONOptions, registry *bson.
657660
doc = bsoncore.AppendValueElement(doc, "hint", hintVal)
658661
}
659662

663+
if d.sort != nil {
664+
if isUnorderedMap(d.sort) {
665+
return nil, ErrMapForOrderedArgument{"sort"}
666+
}
667+
sortVal, err := marshalValue(d.sort, bsonOpts, registry)
668+
if err != nil {
669+
return nil, err
670+
}
671+
doc = bsoncore.AppendValueElement(doc, "sort", sortVal)
672+
}
673+
660674
return bsoncore.AppendDocumentEnd(doc, uidx)
661675
}
662676

mongo/client_bulk_write_models.go

+18
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type ClientUpdateOneModel struct {
5252
Update interface{}
5353
ArrayFilters []interface{}
5454
Hint interface{}
55+
Sort interface{}
5556
}
5657

5758
// NewClientUpdateOneModel creates a new ClientUpdateOneModel.
@@ -105,6 +106,14 @@ func (uom *ClientUpdateOneModel) SetUpsert(upsert bool) *ClientUpdateOneModel {
105106
return uom
106107
}
107108

109+
// SetSort specifies which document the operation updates if the query matches multiple documents. The first document
110+
// matched by the sort order will be updated. This option is only valid for MongoDB versions >= 8.0. The driver will
111+
// return an error if the sort parameter is a multi-key map. The default value is nil.
112+
func (uom *ClientUpdateOneModel) SetSort(sort interface{}) *ClientUpdateOneModel {
113+
uom.Sort = sort
114+
return uom
115+
}
116+
108117
// ClientUpdateManyModel is used to update multiple documents in a client-level BulkWrite operation.
109118
//
110119
// See corresponding setter methods for documentation.
@@ -176,6 +185,7 @@ type ClientReplaceOneModel struct {
176185
Filter interface{}
177186
Replacement interface{}
178187
Hint interface{}
188+
Sort interface{}
179189
}
180190

181191
// NewClientReplaceOneModel creates a new ClientReplaceOneModel.
@@ -222,6 +232,14 @@ func (rom *ClientReplaceOneModel) SetUpsert(upsert bool) *ClientReplaceOneModel
222232
return rom
223233
}
224234

235+
// SetSort specifies which document the operation replaces if the query matches multiple documents. The first document
236+
// matched by the sort order will be replaced. This option is only valid for MongoDB versions >= 8.0. The driver will
237+
// return an error if the sort parameter is a multi-key map. The default value is nil.
238+
func (rom *ClientReplaceOneModel) SetSort(sort interface{}) *ClientReplaceOneModel {
239+
rom.Sort = sort
240+
return rom
241+
}
242+
225243
// ClientDeleteOneModel is used to delete at most one document in a client-level BulkWriteOperation.
226244
//
227245
// See corresponding setter methods for documentation.

0 commit comments

Comments
 (0)