Skip to content

Commit 720f5e5

Browse files
committed
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
1 parent e87e154 commit 720f5e5

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/bulk/common.js

+16
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ class FindOperators {
591591
*
592592
* @method
593593
* @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}
594+
* @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.
594595
* @throws {MongoError} If operation cannot be added to bulk write
595596
* @return {OrderedBulkOperation|UnorderedBulkOperation} A reference to the parent BulkOperation
596597
*/
@@ -606,6 +607,10 @@ class FindOperators {
606607
upsert: upsert
607608
};
608609

610+
if (updateDocument.hint) {
611+
document.hint = updateDocument.hint;
612+
}
613+
609614
// Clear out current Op
610615
this.s.currentOp = null;
611616
return this.s.options.addToOperationsList(this, UPDATE, document);
@@ -616,6 +621,7 @@ class FindOperators {
616621
*
617622
* @method
618623
* @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}
624+
* @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.
619625
* @throws {MongoError} If operation cannot be added to bulk write
620626
* @return {OrderedBulkOperation|UnorderedBulkOperation} A reference to the parent BulkOperation
621627
*/
@@ -631,6 +637,10 @@ class FindOperators {
631637
upsert: upsert
632638
};
633639

640+
if (updateDocument.hint) {
641+
document.hint = updateDocument.hint;
642+
}
643+
634644
// Clear out current Op
635645
this.s.currentOp = null;
636646
return this.s.options.addToOperationsList(this, UPDATE, document);
@@ -904,6 +914,7 @@ class BulkOperationBase {
904914
*
905915
* @method
906916
* @param {object} op The raw operation to perform.
917+
* @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.
907918
* @return {BulkOperationBase} A reference to self
908919
*/
909920
raw(op) {
@@ -933,6 +944,11 @@ class BulkOperationBase {
933944
u: op[key].update || op[key].replacement,
934945
multi: multi
935946
};
947+
948+
if (op[key].hint) {
949+
operation.hint = op[key].hint;
950+
}
951+
936952
if (this.isOrdered) {
937953
operation.upsert = op[key].upsert ? true : false;
938954
if (op.collation) operation.collation = op.collation;

lib/collection.js

+4
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ Collection.prototype.insert = deprecate(function(docs, options, callback) {
690690
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
691691
* @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
692692
* @param {ClientSession} [options.session] optional session to use for this operation
693+
* @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.
693694
* @param {Collection~updateWriteOpCallback} [callback] The command result callback
694695
* @return {Promise} returns Promise if no callback passed
695696
*/
@@ -728,6 +729,7 @@ Collection.prototype.updateOne = function(filter, update, options, callback) {
728729
* @param {boolean} [options.j=false] Specify a journal write concern.
729730
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
730731
* @param {ClientSession} [options.session] optional session to use for this operation
732+
* @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.
731733
* @param {Collection~updateWriteOpCallback} [callback] The command result callback
732734
* @return {Promise<Collection~updateWriteOpResult>} returns Promise if no callback passed
733735
*/
@@ -758,6 +760,7 @@ Collection.prototype.replaceOne = function(filter, doc, options, callback) {
758760
* @param {boolean} [options.j=false] Specify a journal write concern.
759761
* @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
760762
* @param {ClientSession} [options.session] optional session to use for this operation
763+
* @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.
761764
* @param {Collection~updateWriteOpCallback} [callback] The command result callback
762765
* @return {Promise<Collection~updateWriteOpResult>} returns Promise if no callback passed
763766
*/
@@ -799,6 +802,7 @@ Collection.prototype.updateMany = function(filter, update, options, callback) {
799802
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
800803
* @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
801804
* @param {ClientSession} [options.session] optional session to use for this operation
805+
* @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.
802806
* @param {Collection~writeOpCallback} [callback] The command result callback
803807
* @throws {MongoError}
804808
* @return {Promise} returns Promise if no callback passed

lib/operations/common_functions.js

+4
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ function updateDocuments(coll, selector, document, options, callback) {
347347
op.upsert = options.upsert !== void 0 ? !!options.upsert : false;
348348
op.multi = options.multi !== void 0 ? !!options.multi : false;
349349

350+
if (options.hint) {
351+
op.hint = options.hint;
352+
}
353+
350354
if (finalOptions.arrayFilters) {
351355
op.arrayFilters = finalOptions.arrayFilters;
352356
delete finalOptions.arrayFilters;

0 commit comments

Comments
 (0)