$linuxjunkies
>

mongosh(1)

Interactive MongoDB shell for connecting to and querying MongoDB databases.

UbuntuDebianFedoraArch

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

FlagWhat 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
--tlsEnable TLS/SSL encryption for connection
--tlsCertificateKeyFile <path>Path to TLS certificate and key file
--tlsCAFile <path>Path to certificate authority file
--nodbStart shell without connecting to a database

Examples

Connect to MongoDB on localhost:27017 and open interactive shell

mongosh

Connect to remote MongoDB server using connection URI

mongosh mongodb://mongodb.example.com:27017

Connect with username/password authentication to specific database (prompts for password)

mongosh -u admin -p --host db.example.com -d myapp

Execute 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.js

Start shell in disconnected mode for testing JavaScript without database

mongosh --nodb

Connect 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

Related commands