Docs Menu
Docs Home
/
MongoDB Manual

Store a JavaScript Function on the Server

On this page

  • Before you Begin
  • About this Task
  • Steps

Important

Starting in MongoDB 8.0, server side JavaScript is deprecated. system.js functionality may not work with all features.

There is a special system collection named system.js that can store JavaScript functions for reuse.

This task uses the legacy mongo shell to load server side functions from the system.js collection. This version of the shell is no longer supported. For an alternative solution in the MongoDB Shell, see Write Scripts.

Consider the follow recommendations when using system.js:

  • Do not store application logic in the database.

  • There are performance limitations to running JavaScript inside of MongoDB.

  • Application code is most effective when it shares version control with the application.

To store a function, insert the function into the system.js collection, as in these examples:

1
db.test_numbers.insertMany([
{ value: 1 },
{ value: 2 },
{ value: 3 },
{ value: 4 },
{ value: 5 },
{ value: 6 }
])
2

To store JavaScript functions in the database, insert a document with these fields:

  • The _id field holds the name of the function and is unique per database.

  • The value field holds the function definition.

The following example creates an echo function in the system.js collection:

db.system.js.insertOne(
{
_id: "echo",
value : function(x) { return x; }
}
)

The following example creates an isEven function in the system.js collection:

db.system.js.insertOne(
{
_id: "isEven",
value: function (num) {
return num % 2 === 0;
}
}
)

These functions, saved as BSON type, are available for use from any JavaScript context, such as mapReduce and $where.

Note

Functions saved as the deprecated BSON type JavaScript (with scope), cannot be used by mapReduce and $where.

3
db.loadServerScripts()
4
  1. The following code example runs the echo function stored in system.js:

    echo("test")
    test
  2. The following code example runs the isEven function stored in system.js in the $where operator on the test_numbers collection:

    db.test_numbers.find({
    $where: function () {
    return isEven(this.value);
    }
    })
    { "_id" : ObjectId("668d7be41b55bec1bf191499"), "value" : 2 }
    { "_id" : ObjectId("668d7be41b55bec1bf19149b"), "value" : 4 }
    { "_id" : ObjectId("668d7be41b55bec1bf19149d"), "value" : 6 }

Next

What is MongoDB?