(Run inside mongosh unless specified)
| Command | Description |
|---|---|
mongosh |
Start MongoDB shell |
mongosh --host <host> --port <port> |
Connect to specific host/port |
mongosh "mongodb://user:pass@localhost:27017/db" |
Connect with auth |
exit or Ctrl+C |
Exit shell |
cls |
Clear screen |
db.version() |
Show MongoDB server version |
db.stats() |
Current database stats |
db.hostInfo() |
Host system info |
db.serverStatus() |
Server status metrics |
| Command | Description |
|---|---|
use <db_name> |
Switch to or create database |
show dbs |
List all databases |
show databases |
Same as show dbs |
db |
Show current database |
db.dropDatabase() |
Delete current database |
db.createCollection("name") |
Explicitly create collection |
db.getCollectionNames() |
List collections in current DB |
show collections |
List collections |
db.getCollection("coll") |
Get collection reference |
db.stats() |
Database stats (size, collections, etc.) |
| Command | Description |
|---|---|
db.createCollection("students", { capped: true, size: 100000 }) |
Create capped collection |
db.<coll>.drop() |
Drop collection |
db.<coll>.renameCollection("newname") |
Rename collection |
db.<coll>.stats() |
Collection stats |
db.<coll>.storageSize() |
Storage size in bytes |
db.<coll>.totalSize() |
Total size (data + indexes) |
db.<coll>.dataSize() |
Data size only |
db.<coll>.countDocuments({}) |
Count documents (accurate) |
db.<coll>.estimatedDocumentCount() |
Fast approximate count |
db.<coll>.find().count() |
Deprecated — avoid |
| Command | Description |
|---|---|
db.<coll>.insertOne({ doc }) |
Insert one document |
db.<coll>.insertMany([doc1, doc2]) |
Insert multiple |
db.<coll>.insert({ doc }) |
Legacy — works but not recommended |
Returns:
_idof inserted document(s)
| Command | Description |
|---|---|
db.<coll>.find() |
Find all documents |
db.<coll>.find().pretty() |
Formatted output |
db.<coll>.findOne() |
Return first matching document |
db.<coll>.find({ field: value }) |
Query with filter |
db.<coll>.find({ age: { $gt: 18 } }) |
Comparison query |
db.coll.find({}, { name: 1, _id: 0 }) // Include name, exclude _iddb.coll.find().sort({ age: -1 }) // Descending
db.coll.find().sort({ name: 1 }) // Ascendingdb.coll.find().limit(10)
db.coll.find().skip(5)| Method | Description |
|---|---|
.forEach(doc => printjson(doc)) |
Iterate results |
.toArray() |
Convert cursor to array |
.hasNext() / .next() |
Manual cursor control |
| Command | Description |
|---|---|
db.<coll>.updateOne(filter, update, options) |
Update first match |
db.<coll>.updateMany(filter, update) |
Update all matches |
db.<coll>.replaceOne(filter, replacement) |
Replace entire doc |
| Operator | Use |
|---|---|
$set |
Set field value |
$unset |
Remove field |
$inc |
Increment number |
$mul |
Multiply number |
$rename |
Rename field |
$push |
Add to array |
$pop |
Remove from array end |
$pull |
Remove by value |
$addToSet |
Add if not exists |
$currentDate |
Set to current time |
Example:
db.students.updateOne(
{ name: "Ami" },
{ $set: { age: 18 }, $push: { hobbies: "Coding" } }
)Use
{ upsert: true }to insert if not found
| Command | Description |
|---|---|
db.<coll>.deleteOne(filter) |
Delete first match |
db.<coll>.deleteMany(filter) |
Delete all matches |
db.<coll>.remove(filter, { justOne: true }) |
Legacy |
| Stage | Command |
|---|---|
$match |
Filter documents |
$project |
Reshape (include/exclude/rename) |
$group |
Group by field |
$sort |
Sort results |
$limit / $skip |
Pagination |
$unwind |
Deconstruct array |
$lookup |
Join with another collection |
$addFields |
Add computed fields |
$count |
Count documents |
$out / $merge |
Write output |
Example:
db.sales.aggregate([
{ $match: { status: "A" } },
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } },
{ $sort: { total: -1 } },
{ $limit: 5 }
])| Command | Description |
|---|---|
db.<coll>.createIndex({ field: 1 }) |
Ascending index |
db.<coll>.createIndex({ field: -1 }) |
Descending |
db.<coll>.createIndex({ a: 1, b: 1 }) |
Compound |
db.<coll>.createIndex({ loc: "2dsphere" }) |
Geospatial |
db.<coll>.createIndex({ text: "text" }) |
Text index |
db.<coll>.createIndex({ field: 1 }, { unique: true }) |
Unique |
db.<coll>.createIndex({ field: 1 }, { name: "idx_name" }) |
Named index |
| Command | Description |
|---|---|
db.<coll>.getIndexes() |
List all indexes |
db.<coll>.dropIndex("index_name") |
Drop by name |
db.<coll>.dropIndexes() |
Drop all non-_id indexes |
db.<coll>.totalIndexSize() |
Total index size |
| Command | Description |
|---|---|
db.runCommand({ ping: 1 }) |
Test connection |
db.runCommand({ buildInfo: 1 }) |
Build info |
db.runCommand({ collStats: "coll" }) |
Collection stats |
db.runCommand({ dbStats: 1 }) |
DB stats |
db.runCommand({ serverStatus: 1 }) |
Server metrics |
db.runCommand({ listDatabases: 1 }) |
List all DBs |
db.runCommand({ listCollections: 1 }) |
List collections |
db.adminCommand(...) |
Run on admin DB |
| Command | Description |
|---|---|
use admin |
Switch to admin DB |
db.createUser({ user: "admin", pwd: "pass", roles: ["root"] }) |
Create user |
db.updateUser("user", { pwd: "newpass" }) |
Change password |
db.dropUser("user") |
Delete user |
db.auth("user", "pass") |
Authenticate in shell |
db.getUsers() |
List users |
db.changeUserPassword("user", "new") |
Change password |
| Command | Description |
|---|---|
rs.status() |
Replica set status |
rs.initiate() |
Initialize replica set |
rs.conf() |
Configuration |
rs.add("host:port") |
Add member |
rs.remove("host") |
Remove member |
rs.stepDown() |
Step down primary |
rs.reconfig(cfg) |
Reconfigure |
rs.isMaster() |
Check if primary |
| Command | Description |
|---|---|
sh.enableSharding("db") |
Enable sharding on DB |
sh.shardCollection("db.coll", { key: 1 }) |
Shard collection |
sh.status() |
Sharding status |
sh.addShard("host") |
Add shard |
sh.moveChunk(...) |
Move chunk |
sh.splitAt("db.coll", { key: value }) |
Split chunk |
| CLI Command | Description |
|---|---|
mongodump --db dbname |
Backup database |
mongorestore --db dbname dump/dbname |
Restore |
mongoexport --db db --collection coll --out file.json |
Export to JSON |
mongoimport --db db --collection coll --file file.json |
Import |
| Type | Example |
|---|---|
| String | "Pava" |
| Number | 17, 7.50 |
| Boolean | true |
| Null | null |
| Array | ["Games", "Code"] |
| Object | { name: "Ami" } |
| ObjectId | ObjectId("671a...") |
| Date | ISODate("2025-10-29") |
| Regex | /^A/ |
| Binary | BinData(0, "abc=") |
| Operator | Meaning |
|---|---|
$eq |
Equals |
$ne |
Not equals |
$gt |
Greater than |
$gte |
Greater than or equal |
$lt |
Less than |
$lte |
Less than or equal |
$in |
In array |
$nin |
Not in array |
| Operator | Meaning |
|---|---|
$and |
All true |
$or |
Any true |
$not |
Negate |
$nor |
None true |
| Operator | Meaning |
|---|---|
$exists |
Field exists |
$type |
Field type |
| Operator | Meaning |
|---|---|
$all |
Contains all |
$elemMatch |
Match element in array |
$size |
Array length |
| Operator | Meaning |
|---|---|
$mod |
Modulo |
$regex |
Regex match |
$text |
Text search (with index) |
| Command | Description |
|---|---|
DBQuery.shellBatchSize = 50 |
Set batch size |
it |
Show next batch (after cursor) |
printjson(doc) |
Print formatted |
cursor.explain("executionStats") |
Query plan |
const session = db.getMongo().startSession();
session.startTransaction();
try {
db.coll1.insertOne({ x: 1 }, { session });
db.coll2.updateOne({}, { $inc: { y: 1 } }, { session });
session.commitTransaction();
} catch (e) {
session.abortTransaction();
}
session.endSession();// Core
use db, show dbs, db.dropDatabase()
db.createCollection(), show collections
// CRUD
insertOne(), insertMany()
find(), findOne(), pretty()
updateOne(), updateMany(), replaceOne()
deleteOne(), deleteMany()
// Aggregation
aggregate([ { $match }, { $group }, ... ])
// Index
createIndex(), getIndexes(), dropIndex()
// Admin
db.runCommand({ ping: 1 }), serverStatus()
// Replica Set
rs.status(), rs.initiate()
// Sharding
sh.enableSharding(), sh.shardCollection()
// Auth
db.createUser(), db.auth()