MongoDB CLI Quick Reference

A compact, task-oriented cheat sheet for developers and sysadmins covering everyday mongosh workflows plus core MongoDB CLI tools for import/export, backup, and diagnostics.

Table of Contents

A compact, task‑oriented cheat sheet for day‑to‑day MongoDB work using mongosh and the core command‑line tools. Suitable for developers and sysadmins who already have MongoDB access and want fast recall, not installation steps.


Connect & Basics (mongosh)

Prompt helpers


Databases & Collections

Collection stats & size


CRUD Essentials

Insert

Find

Update

Replace & Upsert

Delete


Query Operators (greatest hits)


Aggregation Pipeline

Common stages: $match, $project, $group, $sort, $limit, $lookup, $unwind, $addFields, $setWindowFields.

Faceted example


Indexing


Transactions (replica set or sharded)


User & Role Management (admin)


Admin & Diagnostics

Replica Set

Sharding (mongos)


Import/Export & Backup Tools (CLI)

BSON dump/restore

JSON/CSV import/export

Stats & monitoring


Performance Tips (quick hits)

  • Always index fields used in $match, join keys for $lookup, and sort prefixes.
  • Prefer projections to reduce network & memory (find({}, {field:1})).
  • Use bulkWrite for many small updates/inserts.
  • Avoid unbounded $lookup and $group without cardinality controls; consider $merge.

Scripting Snippets (mongosh)


BSON Types & ObjectId


Environment & Auth Nuggets


Common One‑Liners


Version Awareness

Shell helpers and server commands evolve. When in doubt, check db.version(), db.hello() vs db.isMaster(), and tool flags via --help on your installed version.

Have a project or a problem?

Talk with a senior engineer for practical recommendations—no obligation.

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Categories

Get a free consultation from Reliable Penguin

Submit the form—or for immediate service call 866-649-7984.