MongoDB modifies data with (/), (/ with operators), and (/) methods. Updates use rather than replacing whole documents — an important distinction.
MongoDB modifies data with (/), (/ with operators), and (/) methods. Updates use rather than replacing whole documents — an important distinction.
insertOneinsertManyupdateOneupdateManydeleteOnedeleteManydb.users.insertOne({ name: "Ann", age: 30 }); // → returns the inserted _id
db.users.insertMany([{ name: "Bob" }, { name: "Carol" }]); // bulk insert (efficient)
db..(
{ : },
{ : { : , : } }
);
{ : { : value } }
{ : { : } }
{ : { : } }
{ : { : } }
{ : { : } }
{ : { : } }
db.users.updateMany({ active: false }, { $set: { archived: true } }); // update all matching
Important: updates use operators ($set, $inc, $push, etc.) to modify specific fields. Without an operator, you'd replace the entire document — a common pitfall:
// ❌ 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, and delete operations are how applications modify data in MongoDB, fundamental to any data-driven application, so understanding them is essential everyday knowledge.
Knowing the methods (insertOne/insertMany for creating, updateOne/updateMany for modifying, deleteOne/deleteMany for removing) covers the basics.
The most important and commonly-mistaken point is that MongoDB updates use update operators ($set, , , , etc.) to modify fields — and crucially, (a notorious pitfall where wipes all other fields, leaving only ), so understanding to always use (and the other operators) for field-level updates is essential for correctness.
Knowing the common operators ($set, $inc for counters, $push/$pull/$addToSet for arrays — array operators being especially relevant given MongoDB's array-rich documents) is important everyday knowledge.
Understanding upsert ({ upsert: true } — insert if no match exists, MongoDB's insert-or-update) is valuable for the common insert-or-update pattern.
Also worth noting is the danger of deleteMany({}) (deleting everything — similar to the missing-WHERE danger in SQL).
Since modifying data is fundamental, and since MongoDB's operator-based updates (with the document-replacement pitfall), array operators, and upsert are how you correctly change data, understanding insert/update/delete — especially the critical update-operator distinction (avoiding accidental document replacement) — is essential, must-know knowledge for any MongoDB development, where the operator pitfall in particular is a frequent source of real bugs.
A library of IT interview questions with detailed answers — from Junior to Senior.
Donate$inc$push$pullupdateOne({...}, {age: 31})age$set