MongoDB डेटा को (/), (operators के साथ /), और (/) methods के साथ संशोधित करता है। Updates पूरे documents को प्रतिस्थापित करने के बजाय का उपयोग करते हैं — एक महत्वपूर्ण अंतर।
MongoDB डेटा को (/), (operators के साथ /), और (/) methods के साथ संशोधित करता है। Updates पूरे documents को प्रतिस्थापित करने के बजाय का उपयोग करते हैं — एक महत्वपूर्ण अंतर।
insertOneinsertManyupdateOneupdateManydeleteOnedeleteManydb.users.insertOne({ name: "Ann", age: 30 }); // → returns the inserted _id
db.users.insertMany([{ name: "Bob" }, { name: "Carol" }]); // bulk insert (efficient)
// updateOne — filter (which doc) + update (with OPERATORS)
db.users.updateOne(
{ name: "Ann" },
{ $set: { age: 31, status: "active" } } // $set: change specific fields
);
// common update operators
{ $set: { field: value } } // set/change a field
{ $unset: { field: "" } } // remove a field
{ $inc: { count: 1 } } // increment a number
{ $push: { tags: "new" } } // add to an array
{ $pull: { tags: "old" } } // remove from an array
{ $addToSet: { tags: "x" } } // add to array only if not present
db.users.updateMany({ active: false }, { $set: { archived: true } }); // update all matching
महत्वपूर्ण: updates विशिष्ट fields को संशोधित करने के लिए operators ($set, $inc, $push, आदि) का उपयोग करते हैं। एक operator के बिना, आप पूरे document को प्रतिस्थापित कर देंगे — एक सामान्य कमी:
// ❌ this REPLACES the whole document (loses other fields!)
db.users.updateOne({ name: "Ann" }, { age: 31 }); // Ann now has ONLY age!
// ✅ use $set to change just the field
db.users.updateOne({ name: "Ann" }, { $set: { age: 31 } });
db.users.deleteOne({ name: "Ann" }); // delete the first match
db.users.deleteMany({ active: false }); // delete all matching
db.users.deleteMany({}); // ⚠️ deletes ALL documents!
db.users.updateOne(
{ email: "[email protected]" },
{ $set: { name: "Ann" } },
{ upsert: true } // if no match, INSERT a new document
);
Insert, update, और delete operations वे तरीके हैं जिनसे applications MongoDB में डेटा संशोधित करते हैं, जो किसी भी data-driven application के लिए मौलिक हैं, इसलिए उन्हें समझना आवश्यक रोज़मर्रा का ज्ञान है।
Methods को जानना (बनाने के लिए insertOne/insertMany, संशोधित करने के लिए updateOne/updateMany, हटाने के लिए deleteOne/deleteMany) मूल बातों को कवर करता है।
सबसे महत्वपूर्ण और सामान्य रूप से गलत समझा जाने वाला बिंदु यह है कि MongoDB updates विशिष्ट fields को संशोधित करने के लिए update operators ($set, $inc, $push, $pull, आदि) का उपयोग करते हैं — और महत्वपूर्ण रूप से, operator को भूल जाना पूरे document को प्रतिस्थापित कर देता है (एक कुख्यात कमी जहाँ updateOne({...}, {age: 31}) अन्य सभी fields को मिटा देता है, केवल age को छोड़कर), इसलिए field-level updates के लिए हमेशा $set (और अन्य operators) का उपयोग करना समझना correctness के लिए आवश्यक है।
सामान्य operators को जानना ($set, counters के लिए $inc, arrays के लिए $push/$pull/$addToSet — MongoDB के array-rich documents को देखते हुए array operators विशेष रूप से प्रासंगिक) महत्वपूर्ण रोज़मर्रा का ज्ञान है।
Upsert ({ upsert: true } — यदि कोई match मौजूद न हो तो insert करें, MongoDB का insert-or-update) को समझना सामान्य insert-or-update pattern के लिए मूल्यवान है।
यह भी ध्यान देने योग्य है कि deleteMany({}) का खतरा (सब कुछ हटाना — SQL में missing-WHERE खतरे के समान)।
चूँकि डेटा संशोधित करना मौलिक है, और चूँकि MongoDB के operator-आधारित updates (document-replacement कमी के साथ), array operators, और upsert वे तरीके हैं जिनसे आप डेटा को सही ढंग से बदलते हैं, insert/update/delete को समझना — विशेष रूप से महत्वपूर्ण update-operator अंतर (आकस्मिक document replacement से बचना) — किसी भी MongoDB development के लिए आवश्यक, अवश्य-जानने योग्य ज्ञान है, जहाँ विशेष रूप से operator की कमी वास्तविक bugs का बार-बार होने वाला स्रोत है।