From 720f5e5e72ad1a35b5dfe1f444de59f0575b4cc4 Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Wed, 16 Oct 2019 12:02:11 -0400 Subject: [PATCH] feat(update-hints): add support for `hint` to all update methods 4.2+ supports a `hint` passed to updates. This patch adds support for that, as well as the CRUD tests proving it works. SPEC-1334 --- lib/bulk/common.js | 16 ++++++++++++++++ lib/collection.js | 4 ++++ lib/operations/common_functions.js | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/lib/bulk/common.js b/lib/bulk/common.js index 8f99c25288..f2e1a46b93 100644 --- a/lib/bulk/common.js +++ b/lib/bulk/common.js @@ -591,6 +591,7 @@ class FindOperators { * * @method * @param {object} updateDocument An update field for an update operation. See {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-u u documentation} + * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information. * @throws {MongoError} If operation cannot be added to bulk write * @return {OrderedBulkOperation|UnorderedBulkOperation} A reference to the parent BulkOperation */ @@ -606,6 +607,10 @@ class FindOperators { upsert: upsert }; + if (updateDocument.hint) { + document.hint = updateDocument.hint; + } + // Clear out current Op this.s.currentOp = null; return this.s.options.addToOperationsList(this, UPDATE, document); @@ -616,6 +621,7 @@ class FindOperators { * * @method * @param {object} updateDocument An update field for an update operation. See {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-u u documentation} + * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information. * @throws {MongoError} If operation cannot be added to bulk write * @return {OrderedBulkOperation|UnorderedBulkOperation} A reference to the parent BulkOperation */ @@ -631,6 +637,10 @@ class FindOperators { upsert: upsert }; + if (updateDocument.hint) { + document.hint = updateDocument.hint; + } + // Clear out current Op this.s.currentOp = null; return this.s.options.addToOperationsList(this, UPDATE, document); @@ -904,6 +914,7 @@ class BulkOperationBase { * * @method * @param {object} op The raw operation to perform. + * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information. * @return {BulkOperationBase} A reference to self */ raw(op) { @@ -933,6 +944,11 @@ class BulkOperationBase { u: op[key].update || op[key].replacement, multi: multi }; + + if (op[key].hint) { + operation.hint = op[key].hint; + } + if (this.isOrdered) { operation.upsert = op[key].upsert ? true : false; if (op.collation) operation.collation = op.collation; diff --git a/lib/collection.js b/lib/collection.js index 49edbdbe41..2b5947aa52 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -690,6 +690,7 @@ Collection.prototype.insert = deprecate(function(docs, options, callback) { * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher. * @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators * @param {ClientSession} [options.session] optional session to use for this operation + * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information. * @param {Collection~updateWriteOpCallback} [callback] The command result callback * @return {Promise} returns Promise if no callback passed */ @@ -728,6 +729,7 @@ Collection.prototype.updateOne = function(filter, update, options, callback) { * @param {boolean} [options.j=false] Specify a journal write concern. * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher. * @param {ClientSession} [options.session] optional session to use for this operation + * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information. * @param {Collection~updateWriteOpCallback} [callback] The command result callback * @return {Promise} returns Promise if no callback passed */ @@ -758,6 +760,7 @@ Collection.prototype.replaceOne = function(filter, doc, options, callback) { * @param {boolean} [options.j=false] Specify a journal write concern. * @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators * @param {ClientSession} [options.session] optional session to use for this operation + * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information. * @param {Collection~updateWriteOpCallback} [callback] The command result callback * @return {Promise} returns Promise if no callback passed */ @@ -799,6 +802,7 @@ Collection.prototype.updateMany = function(filter, update, options, callback) { * @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields). * @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators * @param {ClientSession} [options.session] optional session to use for this operation + * @param {object} [options.hint] An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information. * @param {Collection~writeOpCallback} [callback] The command result callback * @throws {MongoError} * @return {Promise} returns Promise if no callback passed diff --git a/lib/operations/common_functions.js b/lib/operations/common_functions.js index 20c2bc9ac7..9aac1fced0 100644 --- a/lib/operations/common_functions.js +++ b/lib/operations/common_functions.js @@ -347,6 +347,10 @@ function updateDocuments(coll, selector, document, options, callback) { op.upsert = options.upsert !== void 0 ? !!options.upsert : false; op.multi = options.multi !== void 0 ? !!options.multi : false; + if (options.hint) { + op.hint = options.hint; + } + if (finalOptions.arrayFilters) { op.arrayFilters = finalOptions.arrayFilters; delete finalOptions.arrayFilters;