From f76635a0b5f268cb52a37afe050a8574630622c0 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Fri, 18 Feb 2022 11:40:56 -0500 Subject: [PATCH] feat(NODE-3866): Add let option to ReplaceOptions for replaceOne operation (#3148) --- src/operations/update.ts | 2 + test/spec/crud/unified/replaceOne-let.json | 207 +++++++++++++++++++++ test/spec/crud/unified/replaceOne-let.yml | 94 ++++++++++ 3 files changed, 303 insertions(+) create mode 100644 test/spec/crud/unified/replaceOne-let.json create mode 100644 test/spec/crud/unified/replaceOne-let.yml diff --git a/src/operations/update.ts b/src/operations/update.ts index 0026245bfb..35e4ca17b0 100644 --- a/src/operations/update.ts +++ b/src/operations/update.ts @@ -222,6 +222,8 @@ export interface ReplaceOptions extends CommandOperationOptions { hint?: string | Document; /** When true, creates a new document if no document matches the query */ upsert?: boolean; + /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ + let?: Document; } /** @internal */ diff --git a/test/spec/crud/unified/replaceOne-let.json b/test/spec/crud/unified/replaceOne-let.json new file mode 100644 index 0000000000..55b345fe20 --- /dev/null +++ b/test/spec/crud/unified/replaceOne-let.json @@ -0,0 +1,207 @@ +{ + "description": "replaceOne-let", + "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 + }, + { + "_id": 2 + } + ] + } + ], + "tests": [ + { + "description": "ReplaceOne with let option", + "runOnRequirements": [ + { + "minServerVersion": "5.0" + } + ], + "operations": [ + { + "name": "replaceOne", + "object": "collection0", + "arguments": { + "filter": { + "$expr": { + "$eq": [ + "$_id", + "$$id" + ] + } + }, + "replacement": { + "x": "foo" + }, + "let": { + "id": 1 + } + }, + "expectResult": { + "matchedCount": 1, + "modifiedCount": 1, + "upsertedCount": 0 + } + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "update": "coll0", + "updates": [ + { + "q": { + "$expr": { + "$eq": [ + "$_id", + "$$id" + ] + } + }, + "u": { + "x": "foo" + } + } + ], + "let": { + "id": 1 + } + } + } + } + ] + } + ], + "outcome": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1, + "x": "foo" + }, + { + "_id": 2 + } + ] + } + ] + }, + { + "description": "ReplaceOne with let option unsupported (server-side error)", + "runOnRequirements": [ + { + "minServerVersion": "3.6.0", + "maxServerVersion": "4.4.99" + } + ], + "operations": [ + { + "name": "replaceOne", + "object": "collection0", + "arguments": { + "filter": { + "$expr": { + "$eq": [ + "$_id", + "$$id" + ] + } + }, + "replacement": { + "x": "foo" + }, + "let": { + "id": 1 + } + }, + "expectError": { + "errorContains": "'update.let' is an unknown field", + "isClientError": false + } + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "update": "coll0", + "updates": [ + { + "q": { + "$expr": { + "$eq": [ + "$_id", + "$$id" + ] + } + }, + "u": { + "x": "foo" + } + } + ], + "let": { + "id": 1 + } + } + } + } + ] + } + ], + "outcome": [ + { + "collectionName": "coll0", + "databaseName": "crud-tests", + "documents": [ + { + "_id": 1 + }, + { + "_id": 2 + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/test/spec/crud/unified/replaceOne-let.yml b/test/spec/crud/unified/replaceOne-let.yml new file mode 100644 index 0000000000..57f79980ef --- /dev/null +++ b/test/spec/crud/unified/replaceOne-let.yml @@ -0,0 +1,94 @@ +description: "replaceOne-let" + +schemaVersion: "1.0" + +createEntities: + - client: + id: &client0 client0 + observeEvents: [ commandStartedEvent ] + - database: + id: &database0 database0 + client: *client0 + databaseName: &database0Name crud-tests + - collection: + id: &collection0 collection0 + database: *database0 + collectionName: &collection0Name coll0 + +initialData: &initialData + - collectionName: *collection0Name + databaseName: *database0Name + documents: + - { _id: 1 } + - { _id: 2 } + +tests: + - description: "ReplaceOne with let option" + runOnRequirements: + - minServerVersion: "5.0" + operations: + - name: replaceOne + object: *collection0 + arguments: + filter: &filter + $expr: + $eq: [ "$_id", "$$id" ] + replacement: &replacement + x: "foo" + let: &let + id: 1 + expectResult: + matchedCount: 1 + modifiedCount: 1 + upsertedCount: 0 + expectEvents: + - client: *client0 + events: + - commandStartedEvent: + command: + update: *collection0Name + updates: + - + q: *filter + u: *replacement + let: *let + outcome: + - + collectionName: *collection0Name + databaseName: *database0Name + documents: + - { _id: 1, x: "foo" } + - { _id: 2 } + + - description: "ReplaceOne with let option unsupported (server-side error)" + runOnRequirements: + - minServerVersion: "3.6.0" + maxServerVersion: "4.4.99" + operations: + - name: replaceOne + object: *collection0 + arguments: + filter: *filter + replacement: *replacement + let: *let + expectError: + errorContains: "'update.let' is an unknown field" + isClientError: false + expectEvents: + - client: *client0 + events: + - commandStartedEvent: + command: + update: *collection0Name + updates: + - + q: *filter + u: *replacement + let: *let + outcome: + - + collectionName: *collection0Name + databaseName: *database0Name + documents: + - { _id: 1 } + - { _id: 2 }