lib/operations/collection_ops.js
function findAndModify(coll, query, sort, doc, options, callback) {
  // Create findAndModify command object
  const queryObject = {
    findAndModify: coll.s.name,
    query: query
  };

  sort = formattedOrderClause(sort);
  if (sort) {
    queryObject.sort = sort;
  }

  queryObject.new = options.new ? true : false;
  queryObject.remove = options.remove ? true : false;
  queryObject.upsert = options.upsert ? true : false;

  const projection = options.projection || options.fields;

  if (projection) {
    queryObject.fields = projection;
  }

  if (options.arrayFilters) {
    queryObject.arrayFilters = options.arrayFilters;
    delete options.arrayFilters;
  }

  if (doc && !options.remove) {
    queryObject.update = doc;
  }

  if (options.maxTimeMS) queryObject.maxTimeMS = options.maxTimeMS;

  // Either use override on the function, or go back to default on either the collection
  // level or db
  options.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;

  // No check on the documents
  options.checkKeys = false;

  // Final options for retryable writes and write concern
  let finalOptions = Object.assign({}, options);
  finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
  finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);

  // Decorate the findAndModify command with the write Concern
  if (finalOptions.writeConcern) {
    queryObject.writeConcern = finalOptions.writeConcern;
  }

  // Have we specified bypassDocumentValidation
  if (finalOptions.bypassDocumentValidation === true) {
    queryObject.bypassDocumentValidation = finalOptions.bypassDocumentValidation;
  }

  finalOptions.readPreference = ReadPreference.primary;

  // Have we specified collation
  try {
    decorateWithCollation(queryObject, coll, finalOptions);
  } catch (err) {
    return callback(err, null);
  }

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

    return handleCallback(callback, null, result);
  });
}
function findAndModify(coll, query, sort, doc, options, callback) {
  // Create findAndModify command object
  const queryObject = {
    findAndModify: coll.s.name,
    query: query
  };

  sort = formattedOrderClause(sort);
  if (sort) {
    queryObject.sort = sort;
  }

  queryObject.new = options.new ? true : false;
  queryObject.remove = options.remove ? true : false;
  queryObject.upsert = options.upsert ? true : false;

  const projection = options.projection || options.fields;

  if (projection) {
    queryObject.fields = projection;
  }

  if (options.arrayFilters) {
    queryObject.arrayFilters = options.arrayFilters;
    delete options.arrayFilters;
  }

  if (doc && !options.remove) {
    queryObject.update = doc;
  }

  if (options.maxTimeMS) queryObject.maxTimeMS = options.maxTimeMS;

  // Either use override on the function, or go back to default on either the collection
  // level or db
  options.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;

  // No check on the documents
  options.checkKeys = false;

  // Final options for retryable writes and write concern
  let finalOptions = Object.assign({}, options);
  finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
  finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);

  // Decorate the findAndModify command with the write Concern
  if (finalOptions.writeConcern) {
    queryObject.writeConcern = finalOptions.writeConcern;
  }

  // Have we specified bypassDocumentValidation
  if (finalOptions.bypassDocumentValidation === true) {
    queryObject.bypassDocumentValidation = finalOptions.bypassDocumentValidation;
  }

  finalOptions.readPreference = ReadPreference.primary;

  // Have we specified collation
  try {
    decorateWithCollation(queryObject, coll, finalOptions);
  } catch (err) {
    return callback(err, null);
  }

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

    return handleCallback(callback, null, result);
  });
}
function findAndModify ( coll , query , sort , doc , options , callback ) { // Create findAndModify command object const queryObject = { findAndModify : coll . s . name , query : query } ; sort = formattedOrderClause ( sort ) ; if ( sort ) { queryObject . sort = sort ; } queryObject . new = options . new ? true : false ; queryObject . remove = options . remove ? true : false ; queryObject . upsert = options . upsert ? true : false ; const projection = options . projection || options . fields ; if ( projection ) { queryObject . fields = projection ; } if ( options . arrayFilters ) { queryObject . arrayFilters = options . arrayFilters ; delete options . arrayFilters ; } if ( doc && ! options . remove ) { queryObject . update = doc ; } if ( options . maxTimeMS ) queryObject . maxTimeMS = options . maxTimeMS ; // Either use override on the function, or go back to default on either the collection // level or db options . serializeFunctions = options . serializeFunctions || coll . s . serializeFunctions ; // No check on the documents options . checkKeys = false ; // Final options for retryable writes and write concern let finalOptions = Object . assign ( { } , options ) ; finalOptions = applyRetryableWrites ( finalOptions , coll . s . db ) ; finalOptions = applyWriteConcern ( finalOptions , { db : coll . s . db , collection : coll } , options ) ; // Decorate the findAndModify command with the write Concern if ( finalOptions . writeConcern ) { queryObject . writeConcern = finalOptions . writeConcern ; } // Have we specified bypassDocumentValidation if ( finalOptions . bypassDocumentValidation === true ) { queryObject . bypassDocumentValidation = finalOptions . bypassDocumentValidation ; } finalOptions . readPreference = ReadPreference . primary ; // Have we specified collation try { decorateWithCollation ( queryObject , coll , finalOptions ) ; } catch ( err ) { return callback ( err , null ) ; } // Execute the command executeCommand ( coll . s . db , queryObject , finalOptions , ( err , result ) => { if ( err ) return handleCallback ( callback , err , null ) ; return handleCallback ( callback , null , result ) ; } ) ; }
Find and update a document.

@method
@param {Collection} a Collection instance.
@param {object} query Query object to locate the object to modify.
@param {array} sort If multiple docs match, choose the first one in the specified sort order as the object to manipulate.
@param {object} doc The fields/vals to be updated.
@param {object} [options] Optional settings. See Collection.prototype.findAndModify for a list of options.
@param {Collection~findAndModifyCallback} [callback] The command result callback
@deprecated use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead
Find and update a document .