Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

mongosh

The MongoDB shell (and mongodump / mongorestore): query JSON documents, manage users and indexes, back up and restore.

Level
DevSecOps service
Group
Databases and messaging
Needs root
No
Package
mongodb-mongosh
Also called
mongodb, mongodump

What it does

MongoDB stores JSON-like documents in collections (port 27017). Many Node.js and Python apps use it, often as MongoDB Atlas or Azure Cosmos DB for MongoDB. mongosh is a JavaScript shell; mongodump / mongorestore do logical backups. Access control must be turned on (security.authorization: enabled) — old open MongoDB servers were a famous source of data leaks.

How to write it

mongosh "mongodb://USER@HOST:27017/DB?authSource=admin" [--eval JS]
mongodump --uri URI --out DIR

Main parts

Shell commands

show dbs / use DB / show collections
Navigate.
db.coll.find(filter).limit(n)
Read documents.
db.coll.insertOne({...}) / updateOne / deleteOne
Change documents.
db.coll.countDocuments(filter)
Count.
db.coll.createIndex({field: 1})
Make an index.
db.createUser({...})
New user with roles.
db.serverStatus() / db.stats()
Health and size.
db.currentOp()
Running operations.

Options 6

Most used

Flags

--eval "JS"
Run code and exit.
--quiet
Less output.
--tls --tlsCAFile FILE
TLS.
-u USER -p
Login (password prompt).
--authenticationDatabase admin
Where the user lives.
--gzip --archive=FILE
One compressed backup file (mongodump).

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Connect

    mongosh "mongodb://admin@db1.lab.local:27017/?authSource=admin"
  2. 2Turn on access control

    cat /etc/mongod.conf
    net:
      bindIp: 127.0.0.1,10.20.2.30
    security:
      authorization: enabled
    systemctl restart mongod
  3. 3Create an app user with one role

    mongosh admin --eval 'db.createUser({user: "shop", pwd: passwordPrompt(), roles: [{role: "readWrite", db: "shop"}]})'
  4. 4Query documents

    mongosh shop --quiet --eval 'db.orders.find({status: "open"}, {_id: 0, id: 1, total: 1}).limit(5)'
    mongosh shop --quiet --eval 'db.orders.countDocuments({status: "open"})'
  5. 5Make an index for a slow query

    mongosh shop --eval 'db.orders.createIndex({status: 1, createdAt: -1})'
    mongosh shop --eval 'db.orders.find({status: "open"}).explain("executionStats").executionStats.totalDocsExamined'
  6. 6Back up and restore

    mongodump --uri "mongodb://backup@db1:27017/?authSource=admin" --gzip --archive=shop-$(date +%F).gz --db shop
    mongorestore --uri "mongodb://admin@db2:27017/?authSource=admin" --gzip --archive=shop-2026-10-27.gz --drop
  7. 7Check server health

    mongosh --quiet --eval 'db.serverStatus().connections'
    mongosh --quiet --eval 'rs.status().members.map(m => [m.name, m.stateStr])'
  8. 8Find long operations

    mongosh --quiet --eval 'db.currentOp({secs_running: {$gt: 10}}).inprog.map(o => [o.opid, o.secs_running, o.ns])'
  9. 9Run in a container for a lab

    podman run -d --name mongo -p 127.0.0.1:27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=labonly docker.io/library/mongo:8.0
  10. 10Use TLS

    mongosh "mongodb://shop@db1.lab.local:27017/shop" --tls --tlsCAFile /etc/pki/tls/certs/lab-ca.crt

Watch out

  • Never run MongoDB without authorization: enabled or bound to 0.0.0.0 on the internet.
  • --drop in mongorestore deletes collections first.

Files

/etc/mongod.conf
Server config.
/var/lib/mongo/
Data (RHEL packages).
~/.mongodb/mongosh/
Shell history (may contain secrets).

Tips to remember

  • Use passwordPrompt() in createUser, so the password is not in the history.

Exit codes

0Success.
1Error.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man mongosh