> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Server-side JavaScript

**Important:**

[Map-reduce](https://www.mongodb.com/docs/manual/core/map-reduce.md#std-label-map-reduce) is deprecated starting in MongoDB 5.0.

MongoDB provides the following operators that perform server-side execution of JavaScript code:

- [`$where`](https://www.mongodb.com/docs/manual/reference/operator/query/where.md#mongodb-query-op.-where) operator that evaluates a JavaScript expression or a function in order to query for documents.

- [`$accumulator`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator.md#mongodb-group-grp.-accumulator) and [`$function`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/function.md#mongodb-expression-exp.-function) aggregation 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`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) to execute the file on the server. This is a good technique for performing batch administrative work. When you run [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.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`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) or [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos), you can either:

- Pass the `--noscripting` option on the command line. See [`mongod --noscripting`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--noscripting) and [`mongos --noscripting`.](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-option-mongos.--noscripting)

- Set [`security.javascriptEnabled`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-security.javascriptEnabled) to 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](https://www.mongodb.com/docs/drivers/) 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.](https://www.mongodb.com/docs/manual/faq/concurrency.md#std-label-faq-concurrency-operations-locks)

### Unsupported Array and String Functions

MongoDB 6.0 upgrades the internal JavaScript engine used for [server-side JavaScript](https://www.mongodb.com/docs/manual/core/server-side-javascript.md#std-label-server-side-javascript), [`$accumulator`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator.md#mongodb-group-grp.-accumulator), [`$function`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/function.md#mongodb-expression-exp.-function), and [`$where`](https://www.mongodb.com/docs/manual/reference/operator/query/where.md#mongodb-query-op.-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`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) and [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) binaries for `ppc64le` do not include a JavaScript engine. As a result, [`$where`](https://www.mongodb.com/docs/manual/reference/operator/query/where.md#mongodb-query-op.-where), [`$accumulator`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator.md#mongodb-group-grp.-accumulator), [`$function`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/function.md#mongodb-expression-exp.-function), and [map-reduce](https://www.mongodb.com/docs/manual/core/map-reduce.md#std-label-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](https://www.mongodb.com/docs/manual/core/aggregation-pipeline.md#std-label-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`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.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`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/hour.md#mongodb-expression-exp.-hour) with an explicit `timezone` argument. Avoid local `Date` methods in JavaScript. For upgrade guidance, see [Compatibility Changes in MongoDB 9.0.](https://www.mongodb.com/docs/manual/release-notes/9.0-compatibility.md#std-label-9.0-compatibility)

### 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.](https://www.mongodb.com/docs/manual/core/server-side-javascript.md#std-label-disable-server-side-js)
