diff --git a/lib/mongo/Mutator.js b/lib/mongo/Mutator.js index b3f6e81d..c545b0ec 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(); } @@ -340,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) {