Server-side JavaScript

$where, $function, $accumulator and mapReduce with an isolated interpreter.

TeselaDB runs the JavaScript of MongoDB queries: $where, $function, $accumulator and mapReduce. It does so with its own interpreter written in Rust that only sees the documents it receives.

Requirements

  • Nodes started without --noscripting.
JavaScript
db.orders.aggregate([
  {
    $addFields: {
      vat: {
        $function: {
          body: "function (total) { return Math.round(total * 0.21 * 100) / 100 }",
          args: ["$total"],
          lang: "js",
        },
      },
    },
  },
]);

Limits

Each run has a time limit and others for steps and memory, as in MongoDB. JavaScript local dates are in UTC and BSON types show with their MongoDB names (NumberLong, ObjectId…).

Turn it off

If you start the nodes with --noscripting (or with security.javascriptEnabled: false in the configuration file), queries with JavaScript answer with an error. They do on every node you start that way, and queries without JavaScript keep working the same.

MongoDB marks mapReduce as deprecated. TeselaDB keeps it because mongosh and the drivers still send it. For new code use the aggregation framework.

Check

A query with $where returns the same documents as its version with operators.

Common problems

  • Queries with JavaScript answer with an error because the nodes started with --noscripting or with security.javascriptEnabled: false.

See also