MongoDB Full Cheat Sheet & Interview Guide
1. Basic MongoDB Operations
- insertOne(): Inserts a single document into a collection.
Example: db.users.insertOne({name: "Alice", age: 22})
Simple use for adding a record (document) to your collection.
- insertMany(): Inserts multiple documents at once.
Example: db.users.insertMany([{name: "Bob"}, {name: "Charlie"}])
- find(): Retrieves data (like SELECT in SQL).
Example: db.users.find() - returns all users.
db.users.find({name: "Alice"}) - filters by condition.
- updateOne(): Updates the first matching document.
Example: db.users.updateOne({name: "Alice"}, {$set: {age: 23}})
Use when updating a specific record.
- updateMany(): Updates all documents matching a condition.
Example: db.users.updateMany({age: {$lt: 18}}, {$set: {status: "minor"}})
- deleteOne(): Deletes a single document.
Example: db.users.deleteOne({name: "Alice"})
- deleteMany(): Deletes all matching documents.
Example: db.users.deleteMany({status: "inactive"})
2. Nested Object and Array Handling
- MongoDB supports nested documents (objects inside documents).
Example: { name: "John", contact: { phone: "12345", city: "NY" } }
MongoDB Full Cheat Sheet & Interview Guide
- Updating nested fields:
db.users.updateOne({"contact.phone": "12345"}, {$set: {"contact.city": "LA"}})
- Pushing to arrays:
db.users.updateOne({name: "John"}, {$push: {hobbies: "Reading"}})
- Pulling from arrays:
db.users.updateOne({name: "John"}, {$pull: {hobbies: "Gaming"}})
3. Mongoose Methods
- Model.create(): Inserts one or many documents.
User.create({name: "Alice"});
- Model.find(): Finds all documents or with conditions.
User.find({age: {$gt: 18}})
- Model.findById(): Finds by document ID.
User.findById("60ad...")
- Model.findOneAndUpdate(): Updates one document and returns it.
User.findOneAndUpdate({name: "John"}, {age: 30})
- Model.updateOne(), updateMany(), deleteOne(), deleteMany() - all work like MongoDB.
- save(): Used with new document instances.
const user = new User({name: "Tom"});
user.save();
MongoDB Full Cheat Sheet & Interview Guide
4. Express.js and Node.js Integration
- Use mongoose.connect() in Node to connect your app.
mongoose.connect("mongodb://localhost:27017/myapp")
- Express CRUD route example:
app.post("/user", async (req, res) => {
const user = await User.create(req.body);
res.send(user);
});
- app.get(), app.put(), app.delete() can be used for RESTful API endpoints.
- Use async/await or .then() to handle MongoDB operations.
5. Mongo Shell Commands
- show dbs - Lists all databases
- use dbname - Switch to database
- show collections - Lists all collections in current DB
- db.collection.find() - Finds documents
- db.collection.insertOne(), updateOne(), deleteOne() - Core ops
6. Interview Questions & Answers
Q1: What is MongoDB?
A: MongoDB is a NoSQL database that stores data in flexible, JSON-like documents.
MongoDB Full Cheat Sheet & Interview Guide
Q2: Difference between updateOne and updateMany?
A: updateOne updates only the first matching document; updateMany updates all.
Q3: What are embedded and referenced documents?
A: Embedded = nested directly inside parent; Referenced = separate document linked via ID.
Q4: Advantages of MongoDB?
- Schema flexibility
- Easy to scale
- Good for JSON-like data
Q5: Disadvantages of MongoDB?
- Not ideal for complex joins
- Memory usage can be high if misconfigured