$linuxjunkies
>

mongo(1)

Interactive MongoDB shell for connecting to MongoDB servers and executing database operations.

UbuntuDebianFedoraArch

Synopsis

mongo [OPTIONS] [DATABASE]

Description

The mongo command launches an interactive JavaScript shell that connects to a MongoDB database server. It allows you to execute queries, perform CRUD operations, manage databases and collections, and run administrative tasks directly from the command line.

Note: mongo is deprecated in MongoDB 5.0+; use mongosh instead for newer installations.

Common options

FlagWhat it does
--host <hostname>Specify MongoDB server hostname (default: localhost)
--port <port>Specify MongoDB server port (default: 27017)
-u, --username <username>Authenticate with username
-p, --password <password>Authenticate with password (prompt if omitted)
--authenticationDatabase <dbname>Specify database for authentication
--eval <javascript>Execute JavaScript code and exit
--file <filename.js>Execute JavaScript from file and exit
--sslEnable TLS/SSL connection to server
--sslCAFile <path>Path to CA certificate file for SSL
-v, --verboseIncrease output verbosity

Examples

Connect to MongoDB on localhost:27017, opens interactive shell

mongo

Connect to MongoDB server at specific IP and port

mongo --host 192.168.1.100 --port 27017

Connect with username and password authentication

mongo -u admin -p mypassword --authenticationDatabase admin

Execute a query and exit without opening interactive shell

mongo mydb --eval "db.users.find().count()"

Execute MongoDB operations from a JavaScript file

mongo --file script.js

Create a new collection in the specified database

mongo mydb --eval "db.createCollection('logs')"

Connect securely using SSL/TLS with certificate authority

mongo --ssl --sslCAFile /path/to/ca.pem --host secure-mongo.example.com

Related commands