Updating & Deleting Documents

🍁 MongoDBLesson 6Beginner

Updating and deleting records in MongoDB is managed using structural operators that modify specific fields without replacing entire documents.

1 Updates & Deletes
JavaScript — Write Operations
# Update one field in a matching document ($set)
db.users.updateOne(
  { name: "Balaji" },
  { $set: { email: "new_email@test.com" } }
);

# Increment numeric fields ($inc)
db.users.updateMany(
  { age: { $lt: 30 } },
  { $inc: { age: 1 } }
);

# Delete matching document
db.users.deleteOne({ name: "Balaji" });

# Delete multiple documents
db.users.deleteMany({ age: { $gte: 40 } });
2 Code Challenge
Challenge: Write an update statement that adds a new element "vip" to an array field named tags in a document (hint: use the $push operator).