LinuxCommandLibrary

mongo

Connect to MongoDB shell

TLDR

Connect to a local database on the default port (mongodb://localhost:27017)

$ mongo
copy

Connect to a database
$ mongo --host [host] --port [port] [db_name]
copy

Authenticate using the specified username on the specified database (you will be prompted for a password)
$ mongo --host [host] --port [port] --username [username] --authenticationDatabase [authdb_name] [db_name]
copy

Evaluate a JavaScript expression on a database
$ mongo --eval '[JSON.stringify(db.foo.findOne())]' [db_name]
copy

SYNOPSIS

mongo [options] [database] [script.js]

Where:
  [options]: Specifies connection, authentication, and execution parameters.
  [database]: The database to connect to upon startup. Defaults to 'test'.
  [script.js]: An optional JavaScript file to execute. The shell exits after execution unless --shell is specified.

PARAMETERS

--host <hostname>
    Specifies the MongoDB host to connect to (default: localhost).

--port <port>
    Specifies the port to connect to (default: 27017).

-u <username>, --username <username>
    Provides the username for authentication.

-p <password>, --password <password>
    Provides the password for authentication. Will prompt if omitted.

--authenticationDatabase <dbname>
    Specifies the database where the user's credentials are defined.

--nodb
    Starts the shell without connecting to any database.

--shell
    Keeps the shell open after executing a script file.

--eval <javascript>
    Executes a JavaScript expression and exits.

--quiet
    Suppresses shell output during startup.

--version
    Displays the mongo shell version and exits.

-h, --help
    Displays command-line help information.

DESCRIPTION

The mongo command was the primary command-line interface for interacting with MongoDB instances. It provided a JavaScript-based shell environment, allowing users to perform various operations directly against a MongoDB server. This included executing queries, performing administrative tasks such as creating and managing databases, collections, and users, and running JavaScript scripts for more complex operations or automation. mongo offered a direct and powerful way for developers and administrators to explore and manage their MongoDB data. It allowed for the execution of arbitrary JavaScript code, making it highly flexible for data manipulation and schema exploration. Prior to its deprecation, it was an indispensable tool in the MongoDB ecosystem for direct database access and scripting.

CAVEATS

The mongo shell is deprecated since MongoDB 5.0. It is no longer actively developed or maintained by MongoDB. Users are strongly encouraged to migrate to mongosh, the modern MongoDB Shell, which offers enhanced features, improved user experience, and active development support. Continuing to use mongo may lead to compatibility issues with newer MongoDB server versions and lack support for new features.

<B>DEPRECATION NOTICE</B>

Users should be aware that the mongo shell is deprecated and no longer receives updates or support.

<B>MIGRATION TO MONGOSH</B>

For all current and future MongoDB interactions, it is highly recommended to use mongosh. It provides superior features like syntax highlighting, intelligent autocompletion, improved error handling, and a more consistent API experience.

HISTORY

The mongo shell served as the foundational interactive tool for MongoDB from its early days. It was designed to provide a rich, JavaScript-powered environment for database operations, reflecting MongoDB's document-oriented and flexible data model. For many years, it was the primary way developers and administrators interacted directly with their MongoDB instances. Its development tracked MongoDB server releases, with features added to support new server capabilities. However, as MongoDB evolved, the need for a more robust, user-friendly, and extensible shell became apparent. This led to the development of mongosh, which was introduced as a successor. With the release of MongoDB 5.0, mongo was formally deprecated, marking the end of its active development and encouraging users to transition to the next-generation shell.

SEE ALSO

Copied to clipboard