Skip to content

Commit

Permalink
feat(update-hints): add support for hint to all update methods
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mbroadst committed Oct 21, 2019
1 parent e87e154 commit 720f5e5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/bulk/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
Expand All @@ -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
*/
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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<Collection~updateWriteOpResult>} returns Promise if no callback passed
*/
Expand Down Expand Up @@ -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<Collection~updateWriteOpResult>} returns Promise if no callback passed
*/
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/operations/common_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 720f5e5

Please sign in to comment.