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.
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.