MongoDB Cheatsheet - MongoDB Command Reference

For backend developers using a document store where reads must stay fast. MongoDB shines at schemaless flexible documents, but that freedom costs you unless you index the fields you filter on. By the end you can design CRUD queries against a collection, read explain("executionStats") to confirm an index is used, build an aggregation pipeline with match/group/sort, and know what a replica set guarantees versus what requires sharding.

Databases·43 commands·Last updated 2026-07-21
mongodbnosqlDatabase

Database 6

show dbs
List all databases
use dbname
Switch to a database (created if absent)
db
Show current database
db.stats()
Show current database stats
db.dropDatabase()
Drop current database
show collections
List all collections in current db

Document CRUD 9

db.collection.insertOne({name: "tom"})
Insert one document
db.collection.insertMany([{...}, {...}])
Insert many documents
db.collection.find()
Find all documents
db.collection.find({age: {$gt: 18}})
Conditional query
db.collection.findOne({name: "tom"})
Find one document
db.collection.updateOne({name: "tom"}, {$set: {age: 20}})
Update one document
db.collection.updateMany({age: {$lt: 18}}, {$set: {status: "minor"}})
Update many documents
db.collection.deleteOne({name: "tom"})
Delete one document
db.collection.deleteMany({age: {$lt: 18}})
Delete many documents

Query Operators 8

{field: value}
Exact match
{field: {$gt: value}}
Greater than ($gte = gte)
{field: {$lt: value}}
Less than ($lte = lte)
{field: {$ne: value}}
Not equal
{field: {$in: [v1, v2]}}
In list
{$and: [{...}, {...}]}
AND condition
{$or: [{...}, {...}]}
OR condition
{field: /regex/i}
Regex match

Index 6

db.collection.createIndex({field: 1})
Create ascending index (-1 descending)
db.collection.createIndex({f1: 1, f2: 1})
Compound index
db.collection.createIndex({field: 1}, {unique: true})
Unique index
db.collection.createIndex({field: 1}, {expireAfterSeconds: 3600})
TTL index (auto-expire)
db.collection.getIndexes()
List all indexes on a collection
db.collection.dropIndex("index_name")
Drop an index

Aggregation 7

db.collection.aggregate([{$match: {age: {$gt: 18}}}])
Match (filter) stage
{$group: {_id: "$field", count: {$sum: 1}}}
Group & count
{$project: {name: 1, _id: 0}}
Field projection
{$sort: {field: -1}}
Sort
{$limit: 10} / {$skip: 20}
Limit / skip
{$unwind: "$arrayField"}
Unwind array field
{$lookup: {from: "other", localField: "id", foreignField: "_id", as: "joined"}}
Join (like SQL JOIN)

Replication & Sharding 7

rs.status()
Show replica set status
rs.conf()
Show replica set config
rs.initiate()
Initialize replica set
rs.add("host:port")
Add a member
rs.remove("host:port")
Remove a member
sh.status()
Show sharded cluster status
sh.enableSharding("dbname")
Enable sharding for a database

Tips

  • MongoDB has no fixed schema by default, but use an ORM like Mongoose for validation.
  • Avoid full collection scans — index large collections and use explain() to inspect the plan.
  • Use at least 3 replica-set nodes for HA; an odd count avoids split-brain.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us