Navigation

    Write Scripts for the MongoDB Shell

    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.

    Open a New Connection

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

    note

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

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

    example

    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.

    example

    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>");

    note

    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.

    example

    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");

    Use require() to Include External Files and Modules

    important

    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.

    Require a File

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

    note

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

    example

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

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

    Require a Native Module

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

    example

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

    require('fs')
    var fs = require('fs');

    Require a Non-Native Module

    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.

    example

    Use one of the following commands to include the moment:

    require('moment')
    var moment = require('moment')

    Execute a JavaScript File

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

    example

    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

    note

    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.