mongosh(1)
Interactive MongoDB shell for connecting to and querying MongoDB databases.
Synopsis
mongosh [OPTIONS] [mongodb://connection-string]Description
mongosh is the modern interactive shell for MongoDB, replacing the legacy mongo shell. It provides a JavaScript environment for executing queries, administrative operations, and scripts against MongoDB instances.
Connect to local or remote MongoDB servers, execute CRUD operations, manage databases and collections, and write complex scripts with full JavaScript support including async/await syntax.
The shell automatically connects to localhost:27017 if no connection string is provided, and supports authentication, TLS, replica sets, and sharded clusters.
Common options
| Flag | What it does |
|---|---|
--host <host> | MongoDB server hostname or IP address; default is localhost |
--port <port> | MongoDB server port; default is 27017 |
-u, --username <username> | Username for database authentication |
-p, --password <password> | Password for authentication; omit for prompt |
--authenticationDatabase <database> | Database to authenticate against (usually 'admin') |
-d, --db <database> | Database to connect to after authentication |
--eval <javascript> | Execute JavaScript code and exit |
--file <path> | Execute JavaScript from file and exit |
--tls | Enable TLS/SSL encryption for connection |
--tlsCertificateKeyFile <path> | Path to TLS certificate and key file |
--tlsCAFile <path> | Path to certificate authority file |
--nodb | Start shell without connecting to a database |
Examples
Connect to MongoDB on localhost:27017 and open interactive shell
mongoshConnect to remote MongoDB server using connection URI
mongosh mongodb://mongodb.example.com:27017Connect with username/password authentication to specific database (prompts for password)
mongosh -u admin -p --host db.example.com -d myappExecute a single command to count documents in users collection and exit
mongosh --eval "db.users.countDocuments()"Execute JavaScript file containing MongoDB operations
mongosh --file myscript.jsStart shell in disconnected mode for testing JavaScript without database
mongosh --nodbConnect to MongoDB Atlas cloud cluster with SRV connection string
mongosh "mongodb+srv://user:[email protected]/database"Connect with TLS encryption using custom certificate authority
mongosh --tls --tlsCAFile /path/to/ca.pem