mongosh
Interact with MongoDB databases
TLDR
Connect to a local database on the default port (mongodb://localhost:27017)
Connect to a database
Authenticate using the specified username on the specified database (you will be prompted for a password)
Evaluate a JavaScript expression on a database
SYNOPSIS
mongosh [OPTIONS] [CONNECTION_URI]
PARAMETERS
-h, --host <hostname>
Specifies the hostname of the MongoDB instance to connect to.
--port <port>
Specifies the port number of the MongoDB instance to connect to.
-u, --username <username>
Authenticates as the specified user. Requires a password or password prompt.
-p, --password <password>
Specifies the password for the user. If omitted with --username, mongosh will prompt for the password.
--authenticationDatabase <db>
Specifies the database to authenticate against. Defaults to the database specified in the URI or 'admin'.
--authenticationMechanism <mechanism>
Specifies the authentication mechanism to use (e.g., SCRAM-SHA-256, MONGODB-X509).
--tls, --ssl
Enables TLS/SSL for the connection to the MongoDB instance.
--tlsCAFile <path>
Specifies the path to a .pem file that contains the root certificate chain from the Certificate Authority.
--tlsCertificateKeyFile <path>
Specifies the path to the client's .pem file that contains the client's TLS certificate and private key.
--eval <javascript_code>
Executes the specified JavaScript code string. Useful for short, one-off commands.
--file <file>
Executes the JavaScript file specified by <file> and then exits. Ideal for scripting.
--version
Prints the mongosh version number and exits.
--help
Displays help information about mongosh and its available options.
--nodb
Starts the shell without connecting to any database. Allows executing local JavaScript or connecting later.
--quiet
Runs mongosh in quiet mode, suppressing most shell output, such as connection warnings.
DESCRIPTION
mongosh is the modern, interactive command-line shell for MongoDB. It provides a powerful JavaScript-based interface for interacting with MongoDB deployments, allowing users to execute queries, perform administrative tasks, and write scripts. Designed for both developers and database administrators, mongosh offers features like syntax highlighting, intelligent autocompletion, and context-aware help, significantly enhancing the user experience compared to its predecessor, the legacy mongo shell. It connects directly to MongoDB instances, enabling full CRUD operations, aggregation framework execution, and database administration functionalities such as user management, role definition, and replica set configuration. mongosh is extensible, allowing users to define custom functions and scripts for repetitive tasks. It also supports the connection string URI format for flexible connection options.
CAVEATS
mongosh replaces the legacy mongo shell; while largely compatible, some scripting differences exist due to API and environment changes. It is a standalone executable and does not require Node.js to be separately installed by the user. Users might encounter connection issues related to network configurations, firewalls, or incorrect authentication credentials.
URI CONNECTION STRINGS
mongosh strongly encourages using URI connection strings for specifying connection parameters. A URI combines host, port, database, authentication, and TLS/SSL options into a single string (e.g., mongodb://user:password@host:port/database?authSource=admin&tls=true). This method is more concise and often less error-prone than passing numerous command-line arguments, especially for complex configurations.
INTERACTIVE VS. SCRIPTING MODE
mongosh can be used in two primary modes. In interactive mode (default), it provides a read-eval-print loop (REPL) for ad-hoc queries and administrative tasks, offering features like tab-completion. In scripting mode, invoked using the --file or --eval options, mongosh executes JavaScript code non-interactively and then exits, making it suitable for automation, batch operations, and CI/CD pipelines.
HISTORY
mongosh was introduced by MongoDB Inc. as the official successor to the legacy mongo shell. It emerged around the MongoDB 4.4 and 5.0 releases, aiming to provide a more robust, user-friendly, and independently developed interactive experience. Unlike its predecessor, which was bundled with the MongoDB server, mongosh is distributed as a standalone tool, allowing for more frequent updates and a decoupled development cycle. Its introduction marked a significant upgrade in shell capabilities, including enhanced usability features like syntax highlighting and autocompletion.
SEE ALSO
mongod(1), mongo(1), mongodump(1), mongorestore(1), mongoimport(1), mongoexport(1)