LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

mongosh

modern MongoDB shell

TLDR

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

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.

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

Copied to clipboard
Kai