CURD ___________ Insert ------------------------- db.users.insertOne({ name: "John Doe", email: "john.doe@example.com", age: 30 }) db.users.insertMany([ { name: "Alice", email: "alice@example.com", age: 25 }, { name: "Bob", email: "bob@example.com", age: 28 } ]) Monfo db _id automatically har doccument ko deta hai db.users.insertOne({ _id: 1, name: "David", email: "david@example.com" }) If you attempt to insert a document with a duplicate _id, MongoDB will throw an error: E11000 duplicate key error collectiondb.users.insertOne({ _id: 1, name: "David", email: "david@example.com" }) If you attempt to insert a document with a duplicate _id, MongoDB will throw an error: E11000 duplicate key error collection agar koi csv file hai to wahah is ek bhi bar me insetmany ka istmmal karke data insert kar sakte hai ___________________________________________________________________________ Read ----------- Find Without Criteria db.users.find() ---- sabi document ko read karega Find With Criteria db.users.find({ name: "Alice" }) Projection ka matlab hota hai sirf selected fields dikhana. db.users.find({}, { name: 1, _id: 0 }) 👉 Isme: name: 1 → sirf name show karo _id: 0 → _id hide karo 📌 Result: sirf users ke names dikhenge. ✅ 2. Using Operators Operators filtering ke liye use hote hain: $gt → greater than (>) $lt → less than (<) $eq → equal $ne → not equal db.users.find({ age: { $gt: 25 } }) 👉 Matlab: jin users ki age 25 se zyada ho, sirf wahi lao. agar keval one document chaye db.users.findOne({ name: "Bob" }) db.users.find().sort({ age: 1 }).limit(5) 1. Sort — Order lagana .sort({ age: 1 }) 👉 age: 1 ka matlab: age ko ascending order (choti se badi) me arrange karo. 📌 Agar -1 hota: .sort({ age: -1 }) 2. Limit — Result kam karna .limit(5) 👉 Sirf first 5 documents return honge. Top 5 youngest users ________________________________________________________ Update -------------- db.users.updateOne({ name: "Alice" }, { $set: { age: 26 } }) db.users.updateMany({ age: { $lt: 30 } }, { $set: { status: "active" } }) The $inc operator is used to increment or decrement numeric values. For example: db.users.updateOne({ name: "Alice" }, { $inc: { age: 1 } }) db.users.updateOne({ name: "Alice" }, { $set: { age: 26 } }) 🧠 Agar 5 documents me name: "Alice" match ho jaayein — to kya hoga? 👉 Sirf PEHLA matching document update hoga. Adding a New Attribute If the specified attribute does not exist, MongoDB will add it to the document. For example: db.users.updateOne({ name: "Bob" }, { $set: { city: "New York" } }) Removing an Attribute Use the $unset operator to remove an attribute from a document. For example: db.users.updateOne({ name: "Alice" }, { $unset: { city: "" } }) ______________________________________________________________________________ Delete ---------- deleteOne This method deletes a single document that matches the given criteria. For example: db.users.deleteOne({ name: "Alice" }) deleteMany This method deletes multiple documents that match the specified criteria. For example: db.users.deleteMany({ age: { $lt: 30 } }) Deleting All Documents To delete all documents within a collection, you can use: db.users.deleteMany({})