DBMS Notes
MongoDB is the most popular document-oriented NoSQL database. It stores data as BSON (Binary JSON) documents in collections (analogous to tables), with no...
What is MongoDB?
MongoDB is the most popular document-oriented NoSQL database. It stores data as BSON (Binary JSON) documents in collections (analogous to tables), with no fixed schema required.
Developed by MongoDB Inc. (2007), used by companies like Adobe, eBay, Forbes, and thousands of startups.
Document Structure (BSON/JSON)
Key points:
- Documents can have nested objects and arrays
- Each document can have different fields (schema-flexible)
_idis the primary key — auto-generated as ObjectId if not specified
Basic MongoDB Operations (CRUD)
Create (Insert)
Read (Query)
// Find all documents
db.employees.find();
// Find with condition (WHERE salary > 70000)
db.employees.find({ salary: { $gt: 70000 } });
// Find with multiple conditions (AND)
db.employees.find({ salary: { $gt: 60000 }, department: "CS" });
// Find specific fields (Projection — like SELECT columns)
db.employees.find(
{ department: "CS" },
{ name: 1, salary: 1, _id: 0 } // 1 = include, 0 = exclude
);
// Find one document
db.employees.findOne({ emp_id: "E001" });
// Sort, limit, skip
db.employees.find().sort({ salary: -1 }).limit(5);
// Count
db.employees.countDocuments({ department: "CS" });Update
// Update one document
db.employees.updateOne(
{ emp_id: "E001" }, // filter
{ $set: { salary: 80000 } } // update operation
);
// Update many documents
db.employees.updateMany(
{ department: "CS" },
{ $inc: { salary: 5000 } } // increment salary by 5000
);
// Replace entire document
db.employees.replaceOne(
{ emp_id: "E001" },
{ emp_id: "E001", name: "Alice Johnson", salary: 80000 }
);Delete
// Delete one document
db.employees.deleteOne({ emp_id: "E004" });
// Delete many documents
db.employees.deleteMany({ salary: { $lt: 50000 } });
// Delete all documents in collection
db.employees.deleteMany({});Query Operators
Aggregation Pipeline
MongoDB's aggregation pipeline is equivalent to SQL's GROUP BY, JOIN, HAVING:
Indexes in MongoDB
// Single field index
db.employees.createIndex({ salary: 1 }); // 1=ascending, -1=descending
// Compound index
db.employees.createIndex({ department: 1, salary: -1 });
// Unique index
db.employees.createIndex({ emp_id: 1 }, { unique: true });
// Text index (full-text search)
db.employees.createIndex({ name: "text", skills: "text" });
db.employees.find({ $text: { $search: "Java developer" } });
// View query plan
db.employees.find({ salary: { $gt: 70000 } }).explain("executionStats");Schema Design — Embedding vs Referencing
Embedding (Denormalized)
Store related data inside the same document:
Use embedding when: data is always accessed together; 1:1 or 1:few relationships.
Referencing (Normalized)
Store _id of related document:
Use referencing when: data is large or shared; 1:many or many:many relationships.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for MongoDB — Introduction.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Database Management Systems (DBMS) topic.
Search Terms
dbms, database management systems (dbms), unit, mongodb, introduction, mongodb — introduction
Related Database Management Systems (DBMS) Topics