Important
Map-reduce is deprecated starting in MongoDB 5.0.
MongoDB provides the following operators that perform server-side execution of JavaScript code:
$whereoperator that evaluates a JavaScript expression or a function in order to query for documents.$accumulatorand$functionaggregation operations that allows users to define custom aggregation expressions.
Run .js files with the MongoDB Shell
You can specify a JavaScript (.js) file to mongosh to execute the file on the server. This is a good technique for performing batch administrative work. When you run mongosh on the server, connecting via the localhost interface, the connection is fast with low latency.
Disable Server-Side JavaScript
To disable all server-side execution of JavaScript for mongod or mongos, you can either:
Pass the
--noscriptingoption on the command line. Seemongod --noscriptingandmongos --noscripting.Set
security.javascriptEnabledto false in the configuration file.
Behavior
JavaScript in MongoDB
Although server-side methods use JavaScript, most interactions with MongoDB do not use JavaScript but instead use an idiomatic driver in the language of the interacting application.
Concurrency
Refer to the individual method or operator documentation for any concurrency information. See also the concurrency table.
Unsupported Array and String Functions
MongoDB 6.0 upgrades the internal JavaScript engine used for server-side JavaScript, $accumulator, $function, and $where expressions and from MozJS-60 to MozJS-91. Several deprecated, non-standard array and string functions that existed in MozJS-60 are removed in MozJS-91.
Unavailable on ppc64le
Starting in MongoDB 9.0, server-side JavaScript is not available on the ppc64le architecture. The mongod and mongos binaries for ppc64le do not include a JavaScript engine. As a result, $where, $accumulator, $function, and map-reduce operations fail on that architecture. You cannot enable server-side JavaScript on ppc64le with a configuration file setting or a command-line option.
To run these operations, use a deployment on an architecture that includes a JavaScript engine. You can also rewrite the operations to use aggregation pipeline stages and operators that do not require server-side JavaScript.
Date Operations Use UTC
Starting in MongoDB 9.0, server-side JavaScript evaluates local Date operations in UTC, regardless of the mongod host time zone. Local getters such as Date.prototype.getHours(), local constructors and setters, and operations that convert a date to a string all use UTC. The stored BSON date value is unchanged, as are Date.prototype.getTime(), Date.prototype.toISOString(), and the getUTC*() methods.
To evaluate dates in a named time zone, use an aggregation date operator such as $hour with an explicit timezone argument. Avoid local Date methods in JavaScript. For upgrade guidance, see Compatibility Changes in MongoDB 9.0.
SELinux Considerations
If you use SELinux, any MongoDB operation that requires server-side JavaScript results in a segfault error. To disable execution of server-side JavaScript, see Disable Server-Side JavaScript.