lib/operations/db_ops.js
function evaluate(db, code, parameters, options, callback) {
  let finalCode = code;
  let finalParameters = [];

  // Did the user destroy the topology
  if (db.serverConfig && db.serverConfig.isDestroyed())
    return callback(new MongoError('topology was destroyed'));

  // If not a code object translate to one
  if (!(finalCode && finalCode._bsontype === 'Code')) finalCode = new Code(finalCode);
  // Ensure the parameters are correct
  if (parameters != null && !Array.isArray(parameters) && typeof parameters !== 'function') {
    finalParameters = [parameters];
  } else if (parameters != null && Array.isArray(parameters) && typeof parameters !== 'function') {
    finalParameters = parameters;
  }

  // Create execution selector
  let cmd = { $eval: finalCode, args: finalParameters };
  // Check if the nolock parameter is passed in
  if (options['nolock']) {
    cmd['nolock'] = options['nolock'];
  }

  // Set primary read preference
  options.readPreference = new ReadPreference(ReadPreference.PRIMARY);

  // Execute the command
  executeCommand(db, cmd, options, (err, result) => {
    if (err) return handleCallback(callback, err, null);
    if (result && result.ok === 1) return handleCallback(callback, null, result.retval);
    if (result)
      return handleCallback(
        callback,
        MongoError.create({ message: `eval failed: ${result.errmsg}`, driver: true }),
        null
      );
    handleCallback(callback, err, result);
  });
}
function evaluate(db, code, parameters, options, callback) {
  let finalCode = code;
  let finalParameters = [];

  // Did the user destroy the topology
  if (db.serverConfig && db.serverConfig.isDestroyed())
    return callback(new MongoError('topology was destroyed'));

  // If not a code object translate to one
  if (!(finalCode && finalCode._bsontype === 'Code')) finalCode = new Code(finalCode);
  // Ensure the parameters are correct
  if (parameters != null && !Array.isArray(parameters) && typeof parameters !== 'function') {
    finalParameters = [parameters];
  } else if (parameters != null && Array.isArray(parameters) && typeof parameters !== 'function') {
    finalParameters = parameters;
  }

  // Create execution selector
  let cmd = { $eval: finalCode, args: finalParameters };
  // Check if the nolock parameter is passed in
  if (options['nolock']) {
    cmd['nolock'] = options['nolock'];
  }

  // Set primary read preference
  options.readPreference = new ReadPreference(ReadPreference.PRIMARY);

  // Execute the command
  executeCommand(db, cmd, options, (err, result) => {
    if (err) return handleCallback(callback, err, null);
    if (result && result.ok === 1) return handleCallback(callback, null, result.retval);
    if (result)
      return handleCallback(
        callback,
        MongoError.create({ message: `eval failed: ${result.errmsg}`, driver: true }),
        null
      );
    handleCallback(callback, err, result);
  });
}
function evaluate ( db , code , parameters , options , callback ) { let finalCode = code ; let finalParameters = [ ] ; // Did the user destroy the topology if ( db . serverConfig && db . serverConfig . isDestroyed ( ) ) return callback ( new MongoError ( 'topology was destroyed' ) ) ; // If not a code object translate to one if ( ! ( finalCode && finalCode . _bsontype === 'Code' ) ) finalCode = new Code ( finalCode ) ; // Ensure the parameters are correct if ( parameters != null && ! Array . isArray ( parameters ) && typeof parameters !== 'function' ) { finalParameters = [ parameters ] ; } else if ( parameters != null && Array . isArray ( parameters ) && typeof parameters !== 'function' ) { finalParameters = parameters ; } // Create execution selector let cmd = { $eval : finalCode , args : finalParameters } ; // Check if the nolock parameter is passed in if ( options [ 'nolock' ] ) { cmd [ 'nolock' ] = options [ 'nolock' ] ; } // Set primary read preference options . readPreference = new ReadPreference ( ReadPreference . PRIMARY ) ; // Execute the command executeCommand ( db , cmd , options , ( err , result ) => { if ( err ) return handleCallback ( callback , err , null ) ; if ( result && result . ok === 1 ) return handleCallback ( callback , null , result . retval ) ; if ( result ) return handleCallback ( callback , MongoError . create ( { message : ` ${ result . errmsg } ` , driver : true } ) , null ) ; handleCallback ( callback , err , result ) ; } ) ; }
Evaluate JavaScript on the server

@method
@param {Db} db The Db instance.
@param {Code} code JavaScript to execute on server.
@param {(object|array)} parameters The parameters for the call.
@param {object} [options] Optional settings. See Db.prototype.eval for a list of options.
@param {Db~resultCallback} [callback] The results callback
@deprecated Eval is deprecated on MongoDB 3.2 and forward
Evaluate JavaScript on the server