Skip to content

Commit faee15b

Browse files
committed
feat: support passing a hint to findOneAndReplace/findOneAndUpdate
NODE-2290
1 parent 292fe08 commit faee15b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/collection.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,7 @@ Collection.prototype.findOneAndDelete = function(filter, options, callback) {
16831683
* @param {object} [options] Optional settings.
16841684
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
16851685
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
1686+
* @param {string|object} [options.hint] An optional index to use for this operation
16861687
* @param {number} [options.maxTimeMS] The maximum amount of time to allow the query to run.
16871688
* @param {object} [options.projection] Limits the fields to return for all matching documents.
16881689
* @param {object} [options.sort] Determines which document the operation modifies if the query selects multiple documents.
@@ -1732,6 +1733,7 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
17321733
* @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
17331734
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
17341735
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
1736+
* @param {string|object} [options.hint] An optional index to use for this operation
17351737
* @param {number} [options.maxTimeMS] The maximum amount of time to allow the query to run.
17361738
* @param {object} [options.projection] Limits the fields to return for all matching documents.
17371739
* @param {object} [options.sort] Determines which document the operation modifies if the query selects multiple documents.
@@ -1740,7 +1742,7 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
17401742
* @param {boolean} [options.checkKeys=false] If true, will throw if bson documents start with `$` or include a `.` in any key value
17411743
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
17421744
* @param {boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
1743-
* @param {ClientSession} [options.session] optional session to use for this operation
1745+
* @param {ClientSession} [options.session] An ptional session to use for this operation
17441746
* @param {Collection~findAndModifyCallback} [callback] The collection result callback
17451747
* @return {Promise<Collection~findAndModifyWriteOpResultObject>} returns Promise if no callback passed
17461748
*/

lib/operations/find_and_modify.js

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const executeCommand = require('./db_ops').executeCommand;
88
const formattedOrderClause = require('../utils').formattedOrderClause;
99
const handleCallback = require('../utils').handleCallback;
1010
const ReadPreference = require('../core').ReadPreference;
11+
const maxWireVersion = require('../core/utils').maxWireVersion;
12+
const MongoError = require('../error').MongoError;
1113

1214
class FindAndModifyOperation extends OperationBase {
1315
constructor(collection, query, sort, doc, options) {
@@ -86,6 +88,21 @@ class FindAndModifyOperation extends OperationBase {
8688
return callback(err, null);
8789
}
8890

91+
if (options.hint) {
92+
// TODO: once this method becomes a CommandOperationV2 we will have the server
93+
// in place to check.
94+
const topology = coll.s.topology;
95+
if (maxWireVersion(topology) < 8) {
96+
callback(
97+
new MongoError('The current topology does not support a hint on findAndModify commands')
98+
);
99+
100+
return;
101+
}
102+
103+
queryObject.hint = options.hint;
104+
}
105+
89106
// Execute the command
90107
executeCommand(coll.s.db, queryObject, options, (err, result) => {
91108
if (err) return handleCallback(callback, err, null);

0 commit comments

Comments
 (0)