Skip to content

Commit

Permalink
feat: support passing a hint to findOneAndReplace/findOneAndUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 11, 2020
1 parent 292fe08 commit faee15b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/collection.js
Expand Up @@ -1683,6 +1683,7 @@ Collection.prototype.findOneAndDelete = function(filter, options, callback) {
* @param {object} [options] Optional settings.
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
* @param {string|object} [options.hint] An optional index to use for this operation
* @param {number} [options.maxTimeMS] The maximum amount of time to allow the query to run.
* @param {object} [options.projection] Limits the fields to return for all matching documents.
* @param {object} [options.sort] Determines which document the operation modifies if the query selects multiple documents.
Expand Down Expand Up @@ -1732,6 +1733,7 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
* @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
* @param {string|object} [options.hint] An optional index to use for this operation
* @param {number} [options.maxTimeMS] The maximum amount of time to allow the query to run.
* @param {object} [options.projection] Limits the fields to return for all matching documents.
* @param {object} [options.sort] Determines which document the operation modifies if the query selects multiple documents.
Expand All @@ -1740,7 +1742,7 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
* @param {boolean} [options.checkKeys=false] If true, will throw if bson documents start with `$` or include a `.` in any key value
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
* @param {boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {ClientSession} [options.session] An ptional session to use for this operation
* @param {Collection~findAndModifyCallback} [callback] The collection result callback
* @return {Promise<Collection~findAndModifyWriteOpResultObject>} returns Promise if no callback passed
*/
Expand Down
17 changes: 17 additions & 0 deletions lib/operations/find_and_modify.js
Expand Up @@ -8,6 +8,8 @@ const executeCommand = require('./db_ops').executeCommand;
const formattedOrderClause = require('../utils').formattedOrderClause;
const handleCallback = require('../utils').handleCallback;
const ReadPreference = require('../core').ReadPreference;
const maxWireVersion = require('../core/utils').maxWireVersion;
const MongoError = require('../error').MongoError;

class FindAndModifyOperation extends OperationBase {
constructor(collection, query, sort, doc, options) {
Expand Down Expand Up @@ -86,6 +88,21 @@ class FindAndModifyOperation extends OperationBase {
return callback(err, null);
}

if (options.hint) {
// TODO: once this method becomes a CommandOperationV2 we will have the server
// in place to check.
const topology = coll.s.topology;
if (maxWireVersion(topology) < 8) {
callback(
new MongoError('The current topology does not support a hint on findAndModify commands')
);

return;
}

queryObject.hint = options.hint;
}

// Execute the command
executeCommand(coll.s.db, queryObject, options, (err, result) => {
if (err) return handleCallback(callback, err, null);
Expand Down

0 comments on commit faee15b

Please sign in to comment.