diff --git a/mongo/bulk_write.go b/mongo/bulk_write.go index c10e4b600d..9c386786be 100644 --- a/mongo/bulk_write.go +++ b/mongo/bulk_write.go @@ -337,7 +337,6 @@ func (bw *bulkWrite) runUpdate(ctx context.Context, batch bulkWriteBatch) (opera filter: converted.Filter, update: converted.Replacement, hint: converted.Hint, - sort: converted.Sort, collation: converted.Collation, upsert: converted.Upsert, }.marshal(bw.collection.bsonOpts, bw.collection.registry) @@ -445,7 +444,11 @@ func (doc updateDoc) marshal(bsonOpts *options.BSONOptions, registry *bsoncodec. if doc.multi { updateDoc = bsoncore.AppendBooleanElement(updateDoc, "multi", doc.multi) - } else if doc.sort != nil { + } + if doc.sort != nil { + if isUnorderedMap(doc.sort) { + return nil, ErrMapForOrderedArgument{"sort"} + } s, err := marshal(doc.sort, bsonOpts, registry) if err != nil { return nil, err diff --git a/mongo/bulk_write_models.go b/mongo/bulk_write_models.go index 28d93fb46e..3793e09e27 100644 --- a/mongo/bulk_write_models.go +++ b/mongo/bulk_write_models.go @@ -125,7 +125,6 @@ type ReplaceOneModel struct { Filter interface{} Replacement interface{} Hint interface{} - Sort interface{} } // NewReplaceOneModel creates a new ReplaceOneModel. @@ -240,6 +239,13 @@ func (uom *UpdateOneModel) SetUpsert(upsert bool) *UpdateOneModel { return uom } +// SetSort specifies which document the operation updates if the query matches multiple documents. +// The first document matched by the sort order will be updated. +func (uom *UpdateOneModel) SetSort(sort interface{}) *UpdateOneModel { + uom.Sort = sort + return uom +} + func (*UpdateOneModel) writeModel() {} // UpdateManyModel is used to update multiple documents in a BulkWrite operation. diff --git a/mongo/collection.go b/mongo/collection.go index 2628acddb7..1f3ac8423f 100644 --- a/mongo/collection.go +++ b/mongo/collection.go @@ -758,7 +758,6 @@ func (coll *Collection) ReplaceOne(ctx context.Context, filter interface{}, uOpts.Upsert = opt.Upsert uOpts.Hint = opt.Hint uOpts.Let = opt.Let - uOpts.Sort = opt.Sort uOpts.Comment = opt.Comment updateOptions = append(updateOptions, uOpts) } diff --git a/mongo/integration/crud_helpers_test.go b/mongo/integration/crud_helpers_test.go index 3270dbea33..0ade1f3478 100644 --- a/mongo/integration/crud_helpers_test.go +++ b/mongo/integration/crud_helpers_test.go @@ -1000,8 +1000,6 @@ func executeReplaceOne(mt *mtest.T, sess mongo.Session, args bson.Raw) (*mongo.U opts = opts.SetCollation(createCollation(mt, val.Document())) case "hint": opts = opts.SetHint(createHint(mt, val)) - case "sort": - opts = opts.SetSort(createSort(mt, val)) case "session": default: mt.Fatalf("unrecognized replaceOne option: %v", key) @@ -1142,6 +1140,9 @@ func createBulkWriteModel(mt *mtest.T, rawModel bson.Raw) mongo.WriteModel { if hintVal, err := args.LookupErr("hint"); err == nil { uom.SetHint(createHint(mt, hintVal)) } + if sortVal, err := args.LookupErr("sort"); err == nil { + uom.SetSort(createSort(mt, sortVal)) + } if uom.Upsert == nil { uom.SetUpsert(false) } diff --git a/mongo/integration/unified/bulkwrite_helpers.go b/mongo/integration/unified/bulkwrite_helpers.go index 1d43fca40a..5a12367991 100644 --- a/mongo/integration/unified/bulkwrite_helpers.go +++ b/mongo/integration/unified/bulkwrite_helpers.go @@ -90,6 +90,12 @@ func createBulkWriteModel(rawModel bson.Raw) (mongo.WriteModel, error) { return nil, fmt.Errorf("error creating hint: %w", err) } uom.SetHint(hint) + case "sort": + sort, err := createSort(val) + if err != nil { + return nil, fmt.Errorf("error creating sort: %w", err) + } + uom.SetSort(sort) case "update": update, err = createUpdateValue(val) if err != nil { diff --git a/mongo/integration/unified/crud_helpers.go b/mongo/integration/unified/crud_helpers.go index 1115bda177..7ddbd5502d 100644 --- a/mongo/integration/unified/crud_helpers.go +++ b/mongo/integration/unified/crud_helpers.go @@ -162,6 +162,18 @@ func createHint(val bson.RawValue) (interface{}, error) { return hint, nil } +func createSort(val bson.RawValue) (interface{}, error) { + var sort interface{} + + switch val.Type { + case bsontype.EmbeddedDocument: + sort = val.Document() + default: + return nil, fmt.Errorf("unrecognized sort value type %s", val.Type) + } + return sort, nil +} + func createCommentString(val bson.RawValue) (string, error) { switch val.Type { case bsontype.String: diff --git a/mongo/options/replaceoptions.go b/mongo/options/replaceoptions.go index ecd03dd98f..f7d3960194 100644 --- a/mongo/options/replaceoptions.go +++ b/mongo/options/replaceoptions.go @@ -40,12 +40,6 @@ type ReplaceOptions struct { // Values must be constant or closed expressions that do not reference document fields. Parameters can then be // accessed as variables in an aggregate expression context (e.g. "$$var"). Let interface{} - - // A document specifying which document should be replaced if the filter used by the operation matches multiple - // documents in the collection. If set, the first document in the sorted order will be updated. This option is - // only valid for MongoDB versions >= 8.0. The driver will return an error if the sort parameter is a multi-key - // map. The default value is nil. - Sort interface{} } // Replace creates a new ReplaceOptions instance. @@ -89,12 +83,6 @@ func (ro *ReplaceOptions) SetLet(l interface{}) *ReplaceOptions { return ro } -// SetSort sets the value for the Sort field. -func (ro *ReplaceOptions) SetSort(s interface{}) *ReplaceOptions { - ro.Sort = s - return ro -} - // MergeReplaceOptions combines the given ReplaceOptions instances into a single ReplaceOptions in a last-one-wins // fashion. // @@ -124,9 +112,6 @@ func MergeReplaceOptions(opts ...*ReplaceOptions) *ReplaceOptions { if ro.Let != nil { rOpts.Let = ro.Let } - if ro.Sort != nil { - rOpts.Sort = ro.Sort - } } return rOpts diff --git a/testdata/command-monitoring/updateMany-sort.json b/testdata/command-monitoring/updateMany-sort.json deleted file mode 100644 index 25adf1574a..0000000000 --- a/testdata/command-monitoring/updateMany-sort.json +++ /dev/null @@ -1,125 +0,0 @@ -{ - "description": "updateMany", - "schemaVersion": "1.0", - "runOnRequirements": [ - { - "minServerVersion": "8.0" - } - ], - "createEntities": [ - { - "client": { - "id": "client", - "observeEvents": [ - "commandStartedEvent", - "commandSucceededEvent", - "commandFailedEvent" - ] - } - }, - { - "database": { - "id": "database", - "client": "client", - "databaseName": "command-monitoring-tests" - } - }, - { - "collection": { - "id": "collection", - "database": "database", - "collectionName": "test" - } - } - ], - "initialData": [ - { - "collectionName": "test", - "databaseName": "command-monitoring-tests", - "documents": [ - { - "_id": 1, - "x": 11 - }, - { - "_id": 2, - "x": 22 - }, - { - "_id": 3, - "x": 33 - } - ] - } - ], - "tests": [ - { - "description": "A successful updateMany with sort", - "operations": [ - { - "name": "updateMany", - "object": "collection", - "arguments": { - "filter": { - "_id": { - "$gt": 1 - } - }, - "sort": { - "_id": -1 - }, - "update": { - "$inc": { - "x": 1 - } - } - } - } - ], - "expectEvents": [ - { - "client": "client", - "events": [ - { - "commandStartedEvent": { - "command": { - "update": "test", - "updates": [ - { - "q": { - "_id": { - "$gt": 1 - } - }, - "u": { - "$inc": { - "x": 1 - } - }, - "upsert": { - "$$unsetOrMatches": false - }, - "multi": true - } - ], - "ordered": true - }, - "commandName": "update", - "databaseName": "command-monitoring-tests" - } - }, - { - "commandSucceededEvent": { - "reply": { - "ok": 1, - "n": 2 - }, - "commandName": "update" - } - } - ] - } - ] - } - ] -} \ No newline at end of file diff --git a/testdata/command-monitoring/updateMany-sort.yml b/testdata/command-monitoring/updateMany-sort.yml deleted file mode 100644 index ccbe9a97c7..0000000000 --- a/testdata/command-monitoring/updateMany-sort.yml +++ /dev/null @@ -1,57 +0,0 @@ -description: "updateMany" - -schemaVersion: "1.0" - -runOnRequirements: - - minServerVersion: "8.0" - -createEntities: - - client: - id: &client client - observeEvents: - - commandStartedEvent - - commandSucceededEvent - - commandFailedEvent - - database: - id: &database database - client: *client - databaseName: &databaseName command-monitoring-tests - - collection: - id: &collection collection - database: *database - collectionName: &collectionName test - -initialData: - - collectionName: *collectionName - databaseName: *databaseName - documents: - - { _id: 1, x: 11 } - - { _id: 2, x: 22 } - - { _id: 3, x: 33 } - -tests: - - description: "A successful updateMany with sort" - operations: - - name: updateMany - object: *collection - arguments: - filter: { _id: { $gt: 1 } } - sort: { _id: -1 } - update: { $inc: { x: 1 } } - expectEvents: - - client: *client - events: - - commandStartedEvent: - command: - update: *collectionName - updates: - - q: { _id: { $gt: 1 } } - u: { $inc: { x: 1 } } - upsert: { $$unsetOrMatches: false } - multi: true - ordered: true - commandName: update - databaseName: *databaseName - - commandSucceededEvent: - reply: { ok: 1, n: 2 } - commandName: update diff --git a/testdata/command-monitoring/updateOne-sort.json b/testdata/command-monitoring/updateOne-sort.json deleted file mode 100644 index 3fc9f7bcc7..0000000000 --- a/testdata/command-monitoring/updateOne-sort.json +++ /dev/null @@ -1,130 +0,0 @@ -{ - "description": "updateOne-sort", - "schemaVersion": "1.0", - "runOnRequirements": [ - { - "minServerVersion": "8.0" - } - ], - "createEntities": [ - { - "client": { - "id": "client", - "observeEvents": [ - "commandStartedEvent", - "commandSucceededEvent", - "commandFailedEvent" - ] - } - }, - { - "database": { - "id": "database", - "client": "client", - "databaseName": "command-monitoring-tests" - } - }, - { - "collection": { - "id": "collection", - "database": "database", - "collectionName": "test" - } - } - ], - "initialData": [ - { - "collectionName": "test", - "databaseName": "command-monitoring-tests", - "documents": [ - { - "_id": 1, - "x": 11 - }, - { - "_id": 2, - "x": 22 - }, - { - "_id": 3, - "x": 33 - } - ] - } - ], - "tests": [ - { - "description": "A successful updateOne with sort", - "operations": [ - { - "name": "updateOne", - "object": "collection", - "arguments": { - "filter": { - "_id": { - "$gt": 1 - } - }, - "sort": { - "_id": -1 - }, - "update": { - "$inc": { - "x": 1 - } - } - } - } - ], - "expectEvents": [ - { - "client": "client", - "events": [ - { - "commandStartedEvent": { - "command": { - "update": "test", - "updates": [ - { - "q": { - "_id": { - "$gt": 1 - } - }, - "u": { - "$inc": { - "x": 1 - } - }, - "sort": { - "_id": -1 - }, - "upsert": { - "$$unsetOrMatches": false - }, - "multi": { - "$$unsetOrMatches": false - } - } - ], - "ordered": true - }, - "commandName": "update", - "databaseName": "command-monitoring-tests" - } - }, - { - "commandSucceededEvent": { - "reply": { - "ok": 1, - "n": 1 - }, - "commandName": "update" - } - } - ] - } - ] - } - ] -} \ No newline at end of file diff --git a/testdata/command-monitoring/updateOne-sort.yml b/testdata/command-monitoring/updateOne-sort.yml deleted file mode 100644 index 4653bde72b..0000000000 --- a/testdata/command-monitoring/updateOne-sort.yml +++ /dev/null @@ -1,58 +0,0 @@ -description: "updateOne-sort" - -schemaVersion: "1.0" - -runOnRequirements: - - minServerVersion: "8.0" - -createEntities: - - client: - id: &client client - observeEvents: - - commandStartedEvent - - commandSucceededEvent - - commandFailedEvent - - database: - id: &database database - client: *client - databaseName: &databaseName command-monitoring-tests - - collection: - id: &collection collection - database: *database - collectionName: &collectionName test - -initialData: - - collectionName: *collectionName - databaseName: *databaseName - documents: - - { _id: 1, x: 11 } - - { _id: 2, x: 22 } - - { _id: 3, x: 33 } - -tests: - - description: "A successful updateOne with sort" - operations: - - name: updateOne - object: *collection - arguments: - filter: { _id: { $gt: 1 } } - sort: { _id: -1 } - update: { $inc: { x: 1 } } - expectEvents: - - client: *client - events: - - commandStartedEvent: - command: - update: *collectionName - updates: - - q: { _id: { $gt: 1 } } - u: { $inc: { x: 1 } } - sort: { _id: -1 } - upsert: { $$unsetOrMatches: false } - multi: { $$unsetOrMatches: false } - ordered: true - commandName: update - databaseName: *databaseName - - commandSucceededEvent: - reply: { ok: 1, n: 1 } - commandName: update diff --git a/testdata/crud/unified/bulkWrite-updateOne-sort.json b/testdata/crud/unified/bulkWrite-updateOne-sort.json new file mode 100644 index 0000000000..c00dc4c91e --- /dev/null +++ b/testdata/crud/unified/bulkWrite-updateOne-sort.json @@ -0,0 +1,257 @@ +{ + "description": "BulkWrite updateOne-sort", + "schemaVersion": "1.0", + "createEntities": [ + { + "client": { + "id": "client0", + "observeEvents": [ + "commandStartedEvent", + "commandSucceededEvent" + ] + } + }, + { + "database": { + "id": "database0", + "client": "client0", + "databaseName": "crud-tests" + } + }, + { + "collection": { + "id": "collection0", + "database": "database0", + "collectionName": "coll0" + } + } + ], + "initialData": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1, + "x": 11 + }, + { + "_id": 2, + "x": 22 + }, + { + "_id": 3, + "x": 33 + } + ] + } + ], + "tests": [ + { + "description": "BulkWrite updateOne with sort option", + "runOnRequirements": [ + { + "minServerVersion": "8.0" + } + ], + "operations": [ + { + "object": "collection0", + "name": "bulkWrite", + "arguments": { + "requests": [ + { + "updateOne": { + "filter": { + "_id": { + "$gt": 1 + } + }, + "sort": { + "_id": -1 + }, + "update": [ + { + "$set": { + "x": 1 + } + } + ] + } + } + ] + } + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "update": "coll0", + "updates": [ + { + "q": { + "_id": { + "$gt": 1 + } + }, + "u": [ + { + "$set": { + "x": 1 + } + } + ], + "sort": { + "_id": -1 + }, + "multi": { + "$$unsetOrMatches": false + }, + "upsert": { + "$$unsetOrMatches": false + } + } + ] + } + } + }, + { + "commandSucceededEvent": { + "reply": { + "ok": 1, + "n": 1 + }, + "commandName": "update" + } + } + ] + } + ], + "outcome": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1, + "x": 11 + }, + { + "_id": 2, + "x": 22 + }, + { + "_id": 3, + "x": 1 + } + ] + } + ] + }, + { + "description": "BulkWrite updateOne with sort option unsupported (server-side error)", + "runOnRequirements": [ + { + "minServerVersion": "4.2.0", + "maxServerVersion": "7.99" + } + ], + "operations": [ + { + "object": "collection0", + "name": "bulkWrite", + "arguments": { + "requests": [ + { + "updateOne": { + "filter": { + "_id": { + "$gt": 1 + } + }, + "sort": { + "_id": -1 + }, + "update": [ + { + "$set": { + "x": 1 + } + } + ] + } + } + ] + }, + "expectError": { + "errorContains": "'update.updates.sort' is an unknown field", + "isClientError": false + } + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "update": "coll0", + "updates": [ + { + "q": { + "_id": { + "$gt": 1 + } + }, + "u": [ + { + "$set": { + "x": 1 + } + } + ], + "sort": { + "_id": -1 + }, + "multi": { + "$$unsetOrMatches": false + }, + "upsert": { + "$$unsetOrMatches": false + } + } + ] + } + } + } + ] + } + ], + "outcome": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1, + "x": 11 + }, + { + "_id": 2, + "x": 22 + }, + { + "_id": 3, + "x": 33 + } + ] + } + ] + } + ] +} diff --git a/testdata/crud/unified/bulkWrite-updateOne-sort.yml b/testdata/crud/unified/bulkWrite-updateOne-sort.yml new file mode 100644 index 0000000000..d38d1f1213 --- /dev/null +++ b/testdata/crud/unified/bulkWrite-updateOne-sort.yml @@ -0,0 +1,126 @@ +description: BulkWrite updateOne-sort +schemaVersion: "1.0" +createEntities: + - client: + id: client0 + observeEvents: + - commandStartedEvent + - commandSucceededEvent + - database: + id: database0 + client: client0 + databaseName: crud-tests + - collection: + id: collection0 + database: database0 + collectionName: coll0 +initialData: + - collectionName: coll0 + databaseName: crud-tests + documents: + - _id: 1 + x: 11 + - _id: 2 + x: 22 + - _id: 3 + x: 33 +tests: + - description: BulkWrite updateOne with sort option + runOnRequirements: + - minServerVersion: "8.0" + operations: + - object: collection0 + name: bulkWrite + arguments: + requests: + - updateOne: + filter: + _id: + $gt: 1 + sort: + _id: -1 + update: + - $set: + x: 1 + expectEvents: + - client: client0 + events: + - commandStartedEvent: + command: + update: coll0 + updates: + - q: + _id: + $gt: 1 + u: + - $set: + x: 1 + sort: + _id: -1 + multi: + $$unsetOrMatches: false + upsert: + $$unsetOrMatches: false + - commandSucceededEvent: + reply: { ok: 1, n: 1 } + commandName: update + outcome: + - collectionName: coll0 + databaseName: crud-tests + documents: + - _id: 1 + x: 11 + - _id: 2 + x: 22 + - _id: 3 + x: 1 + - description: BulkWrite updateOne with sort option unsupported (server-side error) + runOnRequirements: + - minServerVersion: 4.2.0 + maxServerVersion: "7.99" + operations: + - object: collection0 + name: bulkWrite + arguments: + requests: + - updateOne: + filter: + _id: + $gt: 1 + sort: + _id: -1 + update: + - $set: + x: 1 + expectError: + errorContains: "'update.updates.sort' is an unknown field" + isClientError: false + expectEvents: + - client: client0 + events: + - commandStartedEvent: + command: + update: coll0 + updates: + - q: + _id: + $gt: 1 + u: + - $set: + x: 1 + sort: + _id: -1 + multi: + $$unsetOrMatches: false + upsert: + $$unsetOrMatches: false + outcome: + - collectionName: coll0 + databaseName: crud-tests + documents: + - _id: 1 + x: 11 + - _id: 2 + x: 22 + - _id: 3 + x: 33 diff --git a/testdata/crud/unified/updateMany-sort.json b/testdata/crud/unified/updateMany-sort.json new file mode 100644 index 0000000000..4cf1f05c63 --- /dev/null +++ b/testdata/crud/unified/updateMany-sort.json @@ -0,0 +1,225 @@ +{ + "description": "updateMany-sort", + "schemaVersion": "1.0", + "createEntities": [ + { + "client": { + "id": "client0", + "observeEvents": [ + "commandStartedEvent" + ] + } + }, + { + "database": { + "id": "database0", + "client": "client0", + "databaseName": "crud-tests" + } + }, + { + "collection": { + "id": "collection0", + "database": "database0", + "collectionName": "coll0" + } + } + ], + "initialData": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1, + "x": 11 + }, + { + "_id": 2, + "x": 22 + }, + { + "_id": 3, + "x": 33 + } + ] + } + ], + "tests": [ + { + "description": "UpdateMany with sort option unsupported", + "runOnRequirements": [ + { + "minServerVersion": "8.0" + } + ], + "operations": [ + { + "name": "updateMany", + "object": "collection0", + "arguments": { + "filter": { + "_id": { + "$gt": 1 + } + }, + "sort": { + "_id": -1 + }, + "update": { + "$inc": { + "x": 1 + } + } + }, + "expectError": { + "isError": true + } + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "update": "coll0", + "updates": [ + { + "q": { + "_id": { + "$gt": 1 + } + }, + "u": { + "$inc": { + "x": 1 + } + }, + "sort": { + "_id": -1 + }, + "multi": true, + "upsert": { + "$$unsetOrMatches": false + } + } + ] + } + } + } + ] + } + ], + "outcome": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1, + "x": 11 + }, + { + "_id": 2, + "x": 22 + }, + { + "_id": 3, + "x": 33 + } + ] + } + ] + }, + { + "description": "updateMany with sort option unsupported (server-side error)", + "runOnRequirements": [ + { + "maxServerVersion": "7.99" + } + ], + "operations": [ + { + "name": "updateMany", + "object": "collection0", + "arguments": { + "filter": { + "_id": { + "$gt": 1 + } + }, + "sort": { + "_id": -1 + }, + "update": { + "$inc": { + "x": 1 + } + } + }, + "expectError": { + "errorContains": "'update.updates.sort' is an unknown field", + "isClientError": false + } + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "update": "coll0", + "updates": [ + { + "q": { + "_id": { + "$gt": 1 + } + }, + "u": { + "$inc": { + "x": 1 + } + }, + "sort": { + "_id": -1 + }, + "multi": true, + "upsert": { + "$$unsetOrMatches": false + } + } + ] + } + } + } + ] + } + ], + "outcome": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1, + "x": 11 + }, + { + "_id": 2, + "x": 22 + }, + { + "_id": 3, + "x": 33 + } + ] + } + ] + } + ] +} diff --git a/testdata/crud/unified/updateMany-sort.yml b/testdata/crud/unified/updateMany-sort.yml new file mode 100644 index 0000000000..c2bda64e9b --- /dev/null +++ b/testdata/crud/unified/updateMany-sort.yml @@ -0,0 +1,117 @@ +description: updateMany-sort +schemaVersion: "1.0" +createEntities: + - client: + id: client0 + observeEvents: + - commandStartedEvent + - database: + id: database0 + client: client0 + databaseName: crud-tests + - collection: + id: collection0 + database: database0 + collectionName: coll0 +initialData: + - collectionName: coll0 + databaseName: crud-tests + documents: + - _id: 1 + x: 11 + - _id: 2 + x: 22 + - _id: 3 + x: 33 +tests: + - description: UpdateMany with sort option unsupported + runOnRequirements: + - minServerVersion: "8.0" + operations: + - name: updateMany + object: collection0 + arguments: + filter: + _id: + $gt: 1 + sort: + _id: -1 + update: + $inc: + x: 1 + expectError: + isError: true + expectEvents: + - client: client0 + events: + - commandStartedEvent: + command: + update: coll0 + updates: + - q: + _id: + $gt: 1 + u: + $inc: + x: 1 + sort: + _id: -1 + multi: true + upsert: + $$unsetOrMatches: false + outcome: + - collectionName: coll0 + databaseName: crud-tests + documents: + - _id: 1 + x: 11 + - _id: 2 + x: 22 + - _id: 3 + x: 33 + - description: updateMany with sort option unsupported (server-side error) + runOnRequirements: + - maxServerVersion: "7.99" + operations: + - name: updateMany + object: collection0 + arguments: + filter: + _id: + $gt: 1 + sort: + _id: -1 + update: + $inc: + x: 1 + expectError: + errorContains: "'update.updates.sort' is an unknown field" + isClientError: false + expectEvents: + - client: client0 + events: + - commandStartedEvent: + command: + update: coll0 + updates: + - q: + _id: + $gt: 1 + u: + $inc: + x: 1 + sort: + _id: -1 + multi: true + upsert: + $$unsetOrMatches: false + outcome: + - collectionName: coll0 + databaseName: crud-tests + documents: + - _id: 1 + x: 11 + - _id: 2 + x: 22 + - _id: 3 + x: 33 diff --git a/testdata/crud/unified/updateOne-sort.json b/testdata/crud/unified/updateOne-sort.json new file mode 100644 index 0000000000..9fe28f69f1 --- /dev/null +++ b/testdata/crud/unified/updateOne-sort.json @@ -0,0 +1,241 @@ +{ + "description": "updateOne-sort", + "schemaVersion": "1.0", + "createEntities": [ + { + "client": { + "id": "client0", + "observeEvents": [ + "commandStartedEvent", + "commandSucceededEvent" + ] + } + }, + { + "database": { + "id": "database0", + "client": "client0", + "databaseName": "crud-tests" + } + }, + { + "collection": { + "id": "collection0", + "database": "database0", + "collectionName": "coll0" + } + } + ], + "initialData": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1, + "x": 11 + }, + { + "_id": 2, + "x": 22 + }, + { + "_id": 3, + "x": 33 + } + ] + } + ], + "tests": [ + { + "description": "UpdateOne with sort option", + "runOnRequirements": [ + { + "minServerVersion": "8.0" + } + ], + "operations": [ + { + "name": "updateOne", + "object": "collection0", + "arguments": { + "filter": { + "_id": { + "$gt": 1 + } + }, + "sort": { + "_id": -1 + }, + "update": { + "$inc": { + "x": 1 + } + } + }, + "expectResult": { + "matchedCount": 1, + "modifiedCount": 1, + "upsertedCount": 0 + } + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "update": "coll0", + "updates": [ + { + "q": { + "_id": { + "$gt": 1 + } + }, + "u": { + "$inc": { + "x": 1 + } + }, + "sort": { + "_id": -1 + }, + "multi": { + "$$unsetOrMatches": false + }, + "upsert": { + "$$unsetOrMatches": false + } + } + ] + } + } + }, + { + "commandSucceededEvent": { + "reply": { + "ok": 1, + "n": 1 + }, + "commandName": "update" + } + } + ] + } + ], + "outcome": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1, + "x": 11 + }, + { + "_id": 2, + "x": 22 + }, + { + "_id": 3, + "x": 34 + } + ] + } + ] + }, + { + "description": "updateOne with sort option unsupported (server-side error)", + "runOnRequirements": [ + { + "maxServerVersion": "7.99" + } + ], + "operations": [ + { + "name": "updateOne", + "object": "collection0", + "arguments": { + "filter": { + "_id": { + "$gt": 1 + } + }, + "sort": { + "_id": -1 + }, + "update": { + "$inc": { + "x": 1 + } + } + }, + "expectError": { + "errorContains": "'update.updates.sort' is an unknown field", + "isClientError": false + } + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "update": "coll0", + "updates": [ + { + "q": { + "_id": { + "$gt": 1 + } + }, + "u": { + "$inc": { + "x": 1 + } + }, + "sort": { + "_id": -1 + }, + "multi": { + "$$unsetOrMatches": false + }, + "upsert": { + "$$unsetOrMatches": false + } + } + ] + } + } + } + ] + } + ], + "outcome": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1, + "x": 11 + }, + { + "_id": 2, + "x": 22 + }, + { + "_id": 3, + "x": 33 + } + ] + } + ] + } + ] +} diff --git a/testdata/crud/unified/updateOne-sort.yml b/testdata/crud/unified/updateOne-sort.yml new file mode 100644 index 0000000000..2354daa7ad --- /dev/null +++ b/testdata/crud/unified/updateOne-sort.yml @@ -0,0 +1,125 @@ +description: updateOne-sort +schemaVersion: "1.0" +createEntities: + - client: + id: client0 + observeEvents: + - commandStartedEvent + - commandSucceededEvent + - database: + id: database0 + client: client0 + databaseName: crud-tests + - collection: + id: collection0 + database: database0 + collectionName: coll0 +initialData: + - collectionName: coll0 + databaseName: crud-tests + documents: + - _id: 1 + x: 11 + - _id: 2 + x: 22 + - _id: 3 + x: 33 +tests: + - description: UpdateOne with sort option + runOnRequirements: + - minServerVersion: "8.0" + operations: + - name: updateOne + object: collection0 + arguments: + filter: + _id: + $gt: 1 + sort: + _id: -1 + update: + $inc: + x: 1 + expectResult: + matchedCount: 1 + modifiedCount: 1 + upsertedCount: 0 + expectEvents: + - client: client0 + events: + - commandStartedEvent: + command: + update: coll0 + updates: + - q: + _id: + $gt: 1 + u: + $inc: + x: 1 + sort: + _id: -1 + multi: + $$unsetOrMatches: false + upsert: + $$unsetOrMatches: false + - commandSucceededEvent: + reply: { ok: 1, n: 1 } + commandName: update + outcome: + - collectionName: coll0 + databaseName: crud-tests + documents: + - _id: 1 + x: 11 + - _id: 2 + x: 22 + - _id: 3 + x: 34 + - description: updateOne with sort option unsupported (server-side error) + runOnRequirements: + - maxServerVersion: "7.99" + operations: + - name: updateOne + object: collection0 + arguments: + filter: + _id: + $gt: 1 + sort: + _id: -1 + update: + $inc: + x: 1 + expectError: + errorContains: "'update.updates.sort' is an unknown field" + isClientError: false + expectEvents: + - client: client0 + events: + - commandStartedEvent: + command: + update: coll0 + updates: + - q: + _id: + $gt: 1 + u: + $inc: + x: 1 + sort: + _id: -1 + multi: + $$unsetOrMatches: false + upsert: + $$unsetOrMatches: false + outcome: + - collectionName: coll0 + databaseName: crud-tests + documents: + - _id: 1 + x: 11 + - _id: 2 + x: 22 + - _id: 3 + x: 33 diff --git a/x/mongo/driver/operation/update.go b/x/mongo/driver/operation/update.go index 95a81410d9..1070e7ca70 100644 --- a/x/mongo/driver/operation/update.go +++ b/x/mongo/driver/operation/update.go @@ -46,7 +46,6 @@ type Update struct { crypt driver.Crypt serverAPI *driver.ServerAPIOptions let bsoncore.Document - sort bsoncore.Document timeout *time.Duration logger *logger.Logger } @@ -205,9 +204,6 @@ func (u *Update) command(dst []byte, desc description.SelectedServer) ([]byte, e if u.let != nil { dst = bsoncore.AppendDocumentElement(dst, "let", u.let) } - if u.sort != nil { - dst = bsoncore.AppendDocumentElement(dst, "sort", u.sort) - } return dst, nil } @@ -401,17 +397,6 @@ func (u *Update) Let(let bsoncore.Document) *Update { return u } -// Sort determines which document the operation updates if the query matches multiple documents. -// The first document matched by the sort order will be updated. -func (u *Update) Sort(sort bsoncore.Document) *Update { - if u == nil { - u = new(Update) - } - - u.sort = sort - return u -} - // Timeout sets the timeout for this operation. func (u *Update) Timeout(timeout *time.Duration) *Update { if u == nil {