lib/mongodb/mongodb/lib/bulk/ordered.js
function executeCommands(bulkOperation, options, callback) {
  if (bulkOperation.s.batches.length === 0) {
    return handleCallback(callback, null, new BulkWriteResult(bulkOperation.s.bulkResult));
  }

  // Ordered execution of the command
  const batch = bulkOperation.s.batches.shift();

  function resultHandler(err, result) {
    // Error is a driver related error not a bulk op error, terminate
    if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) {
      return handleCallback(callback, err);
    }

    // If we have and error
    if (err) err.ok = 0;
    if (err instanceof MongoWriteConcernError) {
      return handleMongoWriteConcernError(batch, bulkOperation.s.bulkResult, true, err, callback);
    }

    // Merge the results together
    const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult);
    const mergeResult = mergeBatchResults(true, batch, bulkOperation.s.bulkResult, err, result);
    if (mergeResult != null) {
      return handleCallback(callback, null, writeResult);
    }

    if (bulkOperation.handleWriteError(callback, writeResult)) return;

    // Execute the next command in line
    executeCommands(bulkOperation, options, callback);
  }

  bulkOperation.finalOptionsHandler({ options, batch, resultHandler }, callback);
}
function executeCommands(bulkOperation, options, callback) {
  if (bulkOperation.s.batches.length === 0) {
    return handleCallback(callback, null, new BulkWriteResult(bulkOperation.s.bulkResult));
  }

  // Ordered execution of the command
  const batch = bulkOperation.s.batches.shift();

  function resultHandler(err, result) {
    // Error is a driver related error not a bulk op error, terminate
    if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) {
      return handleCallback(callback, err);
    }

    // If we have and error
    if (err) err.ok = 0;
    if (err instanceof MongoWriteConcernError) {
      return handleMongoWriteConcernError(batch, bulkOperation.s.bulkResult, true, err, callback);
    }

    // Merge the results together
    const writeResult = new BulkWriteResult(bulkOperation.s.bulkResult);
    const mergeResult = mergeBatchResults(true, batch, bulkOperation.s.bulkResult, err, result);
    if (mergeResult != null) {
      return handleCallback(callback, null, writeResult);
    }

    if (bulkOperation.handleWriteError(callback, writeResult)) return;

    // Execute the next command in line
    executeCommands(bulkOperation, options, callback);
  }

  bulkOperation.finalOptionsHandler({ options, batch, resultHandler }, callback);
}
function executeCommands ( bulkOperation , options , callback ) { if ( bulkOperation . s . batches . length === 0 ) { return handleCallback ( callback , null , new BulkWriteResult ( bulkOperation . s . bulkResult ) ) ; } // Ordered execution of the command const batch = bulkOperation . s . batches . shift ( ) ; function resultHandler ( err , result ) { // Error is a driver related error not a bulk op error, terminate if ( ( ( err && err . driver ) || ( err && err . message ) ) && ! ( err instanceof MongoWriteConcernError ) ) { return handleCallback ( callback , err ) ; } // If we have and error if ( err ) err . ok = 0 ; if ( err instanceof MongoWriteConcernError ) { return handleMongoWriteConcernError ( batch , bulkOperation . s . bulkResult , true , err , callback ) ; } // Merge the results together const writeResult = new BulkWriteResult ( bulkOperation . s . bulkResult ) ; const mergeResult = mergeBatchResults ( true , batch , bulkOperation . s . bulkResult , err , result ) ; if ( mergeResult != null ) { return handleCallback ( callback , null , writeResult ) ; } if ( bulkOperation . handleWriteError ( callback , writeResult ) ) return ; // Execute the next command in line executeCommands ( bulkOperation , options , callback ) ; } bulkOperation . finalOptionsHandler ( { options , batch , resultHandler } , callback ) ; }
Execute next write command in a chain

@param {OrderedBulkOperation} bulkOperation
@param {object} options
@param {function} callback
Execute next write command in a chain