mongosh

modern MongoDB shell

TLDR

Connect to local database
$ mongosh
Connect to specific database
$ mongosh [mongodb://host:27017/dbname]
Connect with authentication
$ mongosh -u [user] -p [password] [dbname]
Execute script file
$ mongosh [dbname] [script.js]
Evaluate expression
$ mongosh --eval "[db.collection.find()]"
Start shell without connecting to a database
$ mongosh --nodb
Connect with TLS to a remote host
$ mongosh --tls "mongodb://[host]:27017/[dbname]"
Quiet mode (suppress startup messages)
$ mongosh --quiet [script.js]

SYNOPSIS

mongosh [options] [connectionstring] [filenames]

DESCRIPTION

mongosh is the modern MongoDB shell, providing a fully functional JavaScript and Node.js REPL environment for interacting with MongoDB deployments.It replaces the legacy mongo shell with improvements including syntax highlighting, intelligent autocomplete, inline help, and extensible snippets. It supports all CRUD operations, aggregation pipelines, database administration, and Atlas connectivity.

PARAMETERS

CONNECTIONSTRING_

MongoDB connection URI.
-u USER
Username for authentication.
-p PASSWORD
Password for authentication.
--eval EXPR
Evaluate JavaScript expression.
--nodb
Start without database connection.
--quiet
Silence non-essential output.
--host HOST
Server hostname (default: localhost).
--port PORT
Server port (default: 27017).
--authenticationDatabase DB
Authentication database (default: admin).
--tls
Enable TLS/SSL connection.
--shell
Force interactive shell after running files/eval.
--help
Display help information.

INSTALL

brew install mongosh
nix profile install nixpkgs#mongosh

CAVEATS

Bundles its own Node.js runtime since version 1.0. Compatible with MongoDB 4.0+, though some features require MongoDB 5.0+. Has a different driver API from the legacy mongo shell; scripts may need updating.

HISTORY

mongosh was released by MongoDB in 2020 as the next-generation shell replacement for the legacy mongo shell.

SEE ALSO

mongo(1), mongod(1), mongoimport(1), mongoexport(1), mongodump(1)