You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Complete MongoDB cheatsheet covering databases, collections, CRUD operations, query/update operators, aggregation pipeline, indexing, data types, performance tuning, and best practices. Includes practical examples for both beginners and advanced developers working with MongoDB.
// Show all databasesshowdbsshowdatabases// Create / Switch to database (use creates if doesn't exist)usemyDatabase// Check current databasedb// Drop current databasedb.dropDatabase()// List databases with sizedb.adminCommand('listDatabases')
4. Collection Operations
// Create collectiondb.createCollection("users")db.createCollection("logs",{capped: true,size: 1048576})// capped with 1MB max// Show collectionsshowcollectionsdb.getCollectionNames()// Drop collectiondb.users.drop()// Rename collectiondb.users.renameCollection("customers")// Check collection statsdb.users.stats()// Check if collection is cappeddb.users.isCapped()
db.users.find({email: {$exists: true}})// Field existsdb.users.find({phone: {$type: "string"}})// Field typedb.users.find({age: {$type: "number"}})
Evaluation Operators
// Regular expressiondb.users.find({name: {$regex: /^John/i}})db.users.find({email: {$regex: /@gmail\.com$/}})// Where (JavaScript expression - use rarely, slower)db.users.find({$where: "this.age > 30"})// Modulodb.users.find({age: {$mod: [5,0]}})// age divisible by 5
Array Operators
// Array with specific elementdb.users.find({tags: "premium"})// Match all elements in arraydb.users.find({tags: {$all: ["premium","verified"]}})// Array sizedb.users.find({tags: {$size: 3}})// Element matchdb.users.find({scores: {$elemMatch: {$gte: 80,$lte: 90}}})// Match by index positiondb.users.find({"tags.0": "admin"})
Reference for "many" relationships (one-to-many/many-to-many)
Avoid unbounded arrays (use referencing instead)
Design for application query patterns
Query Optimization
// Use covered queriesdb.users.createIndex({status: 1,age: 1})db.users.find({status: "active"},{status: 1,age: 1,_id: 0})// Use projection to limit returned fieldsdb.users.find({},{password: 0})// Exclude password// Use limit for paginationdb.users.find().skip(100).limit(20)// Avoid large skips, use range queries instead// Prefer $in over multiple $or conditions// Good:db.users.find({status: {$in: ["active","pending"]}})// Avoid:db.users.find({$or: [{status: "active"},{status: "pending"}]})
Indexing Tips
Create indexes for frequently queried fields
Order matters in compound indexes (equality → sort → range)
Use sparse indexes for optional fields
Avoid indexes with high write load
Monitor index usage with db.collection.aggregate([{ $indexStats: {} }])
Complete MongoDB cheatsheet covering databases, collections, CRUD operations, query/update operators, aggregation pipeline, indexing, data types, performance tuning, and best practices. Includes practical examples for both beginners and advanced developers working with MongoDB.