On this page
db.eval()
On this page
Definition
db.eval( function, arguments )-
Deprecated since version 3.0.
Provides the ability to run JavaScript code on the MongoDB server.
The helper
db.eval()in themongoshell wraps theevalcommand. Therefore, the helper method shares the characteristics and behavior of the underlying command with one exception:db.eval()method does not support thenolockoption.The method accepts the following parameters:
Parameter Type Description functionfunction A JavaScript function to execute. argumentslist Optional. A list of arguments to pass to the JavaScript function. Omit if the function does not take arguments. The JavaScript function need not take any arguments, as in the first example, or may optionally take arguments as in the second:
function () { // ... }function (arg1, arg2) { // ... }
Behavior
You must run db.eval() against the primary member of a replica set. If you attempt to run db.eval() on a secondary member, MongoDB will return an error.
Write Lock
By default, db.eval() takes a global write lock while evaluating the JavaScript function. As a result, db.eval() blocks all other read and write operations to the database while the db.eval() operation runs.
To prevent the taking of the global write lock while evaluating the JavaScript code, use the eval command with nolock set to true. nolock does not impact whether the operations within the JavaScript code take write locks.
For long running db.eval() operation, consider using either the eval command with nolock: true or using other server side code execution options.
Access Control
Changed in version 2.6.
If authorization is enabled, you must have access to all actions on all resources in order to run eval. Providing such access is not recommended, but if your organization requires a user to run eval, create a role that grants anyAction on anyResource. Do not assign this role to any other user.
Examples
The following is an example of the db.eval() method:
db.eval( function(name, incAmount) {
var doc = db.myCollection.findOne( { name : name } );
doc = doc || { name : name , num : 0 , total : 0 , avg : 0 };
doc.num++;
doc.total += incAmount;
doc.avg = doc.total / doc.num;
db.myCollection.save( doc );
return doc;
},
"eliot", 5 );
- The
dbin the function refers to the current database. "eliot"is the argument passed to the function, and corresponds to thenameargument.5is an argument to the function and corresponds to theincAmountfield.
If you want to use the server’s interpreter, you must run db.eval(). Otherwise, the mongo shell’s JavaScript interpreter evaluates functions entered directly into the shell.
If an error occurs, db.eval() throws an exception. The following is an example of an invalid function that uses the variable x without declaring it as an argument:
db.eval( function() { return x + x; }, 3 );
The statement results in the following exception:
{
"errmsg" : "exception: JavaScript execution failed: ReferenceError: x is not defined near '{ return x + x; }' ",
"code" : 16722,
"ok" : 0
}
See also