From 516f060e48bd0d4bfc9bb72a8ba9da2f1f559edb Mon Sep 17 00:00:00 2001 From: Andrei Lisnic Date: Fri, 28 Mar 2025 18:43:27 +0200 Subject: [PATCH 1/4] Avoid making a database query when updating a single document by _id --- lib/mongo/Mutator.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/mongo/Mutator.js b/lib/mongo/Mutator.js index b3f6e81d..ff81f332 100644 --- a/lib/mongo/Mutator.js +++ b/lib/mongo/Mutator.js @@ -139,6 +139,9 @@ export default class Mutator { ...findOptions, fields: {}, }).fetchAsync(); + } else if (Object.keys(selector).length === 1 && typeof selector._id === "string") { + // if we are updating a single document by id, we can skip the find + docs = [{ _id: selector._id }]; } else { docs = await this.find(selector, findOptions).fetchAsync(); } From b7d94c5fa089fb352d9e7f3f5b15749870eee267 Mon Sep 17 00:00:00 2001 From: Andrei Lisnic Date: Fri, 28 Mar 2025 21:05:04 +0200 Subject: [PATCH 2/4] Optimize remove --- lib/mongo/Mutator.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/mongo/Mutator.js b/lib/mongo/Mutator.js index ff81f332..d6abd1dd 100644 --- a/lib/mongo/Mutator.js +++ b/lib/mongo/Mutator.js @@ -343,8 +343,14 @@ export default class Mutator { delete removeOptions.projection; } - // TODO: optimization check if it has _id or _id with {$in} so we don't have to redo this. - const docs = await this.find(selector, removeOptions).fetchAsync(); + let docs + + if (Object.keys(selector).length === 1 && typeof selector._id === "string") { + docs = [{ _id: selector._id }]; + } else { + docs = await this.find(selector, removeOptions).fetchAsync(); + } + let docIds = docs.map((doc) => doc._id); if (!selector._id) { From c2b3e98cddcbc414da1c3d694b872fa686ec5306 Mon Sep 17 00:00:00 2001 From: Andrei Lisnic Date: Fri, 28 Mar 2025 21:13:36 +0200 Subject: [PATCH 3/4] Revert "Optimize remove" This reverts commit b7d94c5fa089fb352d9e7f3f5b15749870eee267. --- lib/mongo/Mutator.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/mongo/Mutator.js b/lib/mongo/Mutator.js index d6abd1dd..ff81f332 100644 --- a/lib/mongo/Mutator.js +++ b/lib/mongo/Mutator.js @@ -343,14 +343,8 @@ export default class Mutator { delete removeOptions.projection; } - let docs - - if (Object.keys(selector).length === 1 && typeof selector._id === "string") { - docs = [{ _id: selector._id }]; - } else { - docs = await this.find(selector, removeOptions).fetchAsync(); - } - + // TODO: optimization check if it has _id or _id with {$in} so we don't have to redo this. + const docs = await this.find(selector, removeOptions).fetchAsync(); let docIds = docs.map((doc) => doc._id); if (!selector._id) { From 0c2c934cfabc48381d3b4077a823eb1bda18bc20 Mon Sep 17 00:00:00 2001 From: Andrei Lisnic Date: Tue, 1 Apr 2025 15:31:16 +0300 Subject: [PATCH 4/4] Optimize remove --- lib/mongo/Mutator.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/mongo/Mutator.js b/lib/mongo/Mutator.js index ff81f332..c545b0ec 100644 --- a/lib/mongo/Mutator.js +++ b/lib/mongo/Mutator.js @@ -343,8 +343,14 @@ export default class Mutator { delete removeOptions.projection; } - // TODO: optimization check if it has _id or _id with {$in} so we don't have to redo this. - const docs = await this.find(selector, removeOptions).fetchAsync(); + let docs + + if (!shouldIncludePrevDocument(this) && Object.keys(selector).length === 1 && typeof selector._id === 'string') { + docs = [{ _id: selector._id }]; + } else { + docs = await this.find(selector, removeOptions).fetchAsync(); + } + let docIds = docs.map((doc) => doc._id); if (!selector._id) {