Write Scripts for mongosh

    You can write scripts for the MongoDB Shell that manipulate data in MongoDB or perform administrative operations.

    This tutorial introduces using the MongoDB Shell with JavaScript to access MongoDB.

    From the MongoDB Shell or from a JavaScript file, you can instantiate database connections using the Mongo() method:

    new Mongo()
    new Mongo(<host>)
    new Mongo(<host:port>)

    The MongoDB Shell does not support the ClientSideFieldLevelEncryptionOptions document with the Mongo() method.

    Consider a MongoDB instance running on localhost on the default port.


    The following example:

    • Instantiates a new connection to the instance, and
    • Sets the global db variable to myDatabase using the Mongo.getDB() method.
    conn = Mongo();
    db = conn.getDB("myDatabase");

    If you connect to a MongoDB instance that enforces access control, you must include the credentials in the connection string.


    The following command connects to a MongoDB instance that is:

    • Running on localhost on the default port, and
    • Secured using SCRAM.
    conn = Mongo("mongodb://<username>:<password>@localhost:27017/<authDB>");

    The MongoDB Shell redacts credentials from the command history and the logs.

    Additionally, you can use the connect() method to connect to the MongoDB instance.


    The following command:

    • Connects to the MongoDB instance that is running on localhost with the non-default port 27020, and
    • Sets the global db variable.
    db = connect("localhost:27020/myDatabase");

    A complete description of Node.js, modules, and the require() function is out of scope for this tutorial. To learn more, refer to the Node.js Documentation.

    You can use the require() function in the MongoDB Shell to include modules which exist in separate files.

    You can require() JavaScript files in the MongoDB Shell without any additional setup or configuration.


    The MongoDB Shell does not execute files imported with require(). The MongoDB Shell adds everything from an imported file to the current execution scope.


    Use one of the following commands include a file from the current working directory named tests.js:

    var tests = require('./tests.js')

    You can require() native Node modules (such as fs) in the MongoDB Shell without any additional setup or configuration.


    Use one of the following commands to include the fs module:

    var fs = require('fs');

    To require() non-native Node modules (such as those downloaded from npm) you must install the module globally or to the node_modules directory in your current working directory.

    Once you install or copy your desired package to one of the module directories, you can require() that package.


    Use one of the following commands to include the moment:

    var moment = require('moment')

    You can execute a .js file from within the MongoDB Shell using the .load command.


    The following command loads and executes the myjstest.js file:

    .load myjstest.js

    The .load command accepts relative and absolute paths. If the current working directory of the MongoDB Shell is /data/db, and myjstest.js resides in the /data/db/scripts directory, then the following calls within the MongoDB Shell are equivalent:

    .load scripts/myjstest.js
    .load /data/db/scripts/myjstest.js

    There is no search path for the .load command. If the desired script is not in the current working directory or the full specified path, the MongoDB Shell cannot access the file.

    Give Feedback