Quick Reference Cheat Sheet
8. Quick Reference Cheat Sheet
Section titled “8. Quick Reference Cheat Sheet”CRUD Commands
Section titled “CRUD Commands”// ─── INSERT ───────────────────────────────db.col.insertOne({ field: "value" })db.col.insertMany([{ ... }, { ... }])
// ─── FIND ─────────────────────────────────db.col.find() // all docsdb.col.find({ field: "value" }) // with filterdb.col.findOne({ field: "value" }) // first matchdb.col.find({}, { field1: 1, _id: 0 }) // projectiondb.col.find().sort({ field: -1 }) // sort descdb.col.find().limit(10).skip(20) // paginationdb.col.countDocuments({ status: "ok" }) // count
// ─── UPDATE ───────────────────────────────db.col.updateOne({ filter }, { $set: { field: val } })db.col.updateMany({ filter }, { $set: { field: val } })db.col.updateOne({ filter }, { $inc: { count: 1 } })db.col.updateOne({ filter }, { $push: { arr: item } })db.col.updateOne({ filter }, { $pull: { arr: item } })db.col.updateOne({ filter }, { $unset: { field: "" } })db.col.findByIdAndUpdate(id, { $set: {...} }, { new: true })
// ─── DELETE ───────────────────────────────db.col.deleteOne({ _id: ObjectId("...") })db.col.deleteMany({ status: "inactive" })db.col.findByIdAndDelete(id)Query Operators
Section titled “Query Operators”// Comparison{ age: { $gt: 18 } } // greater than{ age: { $gte: 18, $lte: 30 }} // between{ status: { $ne: "banned" } } // not equal{ role: { $in: ["a","b"] } } // in list{ role: { $nin: ["x","y"] } } // not in list
// Logical{ $and: [{...}, {...}] } // all true{ $or: [{...}, {...}] } // any true{ field: { $not: {...} } } // negate
// Element{ field: { $exists: true } } // field exists{ field: { $type: "string" } } // type check
// Array{ arr: { $all: ["a","b"] } } // contains all{ arr: { $elemMatch: {...} } } // element matchesAggregation Stages
Section titled “Aggregation Stages”db.col.aggregate([ { $match: { status: "active" } }, { $group: { _id: "$field", total: { $sum: "$amount" } } }, { $sort: { total: -1 } }, { $limit: 10 }, { $skip: 0 }, { $project: { field: 1, _id: 0 } }, { $lookup: { from: "other", localField: "id", foreignField: "_id", as: "data" } }, { $unwind: "$arrayField" }, { $addFields: { newField: { $sum: "$arr" } } }, { $count: "totalCount" }])Mongoose Quick Reference
Section titled “Mongoose Quick Reference”// Model CRUDModel.create(data)Model.find(filter).select('f1 f2').sort({f:-1}).limit(10).skip(0)Model.findById(id)Model.findOne(filter)Model.findByIdAndUpdate(id, {$set:data}, {new:true, runValidators:true})Model.findByIdAndDelete(id)Model.countDocuments(filter)Model.exists(filter)
// Populate.populate('field', 'name email').populate({ path: 'field', select: 'name', populate: { path: 'nested' } })
// Schema Options{ type: String, required: true, unique: true, trim: true, lowercase: true }{ type: Number, min: 0, max: 100, default: 0 }{ type: String, enum: ['a', 'b', 'c'] }{ type: Schema.Types.ObjectId, ref: 'ModelName' }{ timestamps: true } // schema option — auto createdAt & updatedAt
// Middlewareschema.pre('save', function(next) { ... next(); })schema.post('save', function(doc, next) { ... next(); })🍃 These notes cover everything from MongoDB basics to production-level advanced topics. Use them as a reference while building projects, preparing for interviews, or learning from scratch.
Happy coding! 🚀