Deleting Documents
Delete operations remove documents from a collection. After deletion the indexes are automatically rebuilt to stay consistent.
deleteOne
const deleted = users.deleteOne({ name: 'David' });
// returns the deleted document or null
deleteMany
const removed = users.deleteMany({ age: { $lt: 18 } });
console.log(`Removed ${removed.length} underage users`);
To delete everything in a collection:
users.deleteMany({}); // empty filter matches all
Async Versions
await users.deleteOneAsync({ name: 'David' });
await users.deleteManyAsync({ active: false });
Important: After a delete the internal indexes are rebuilt because array positions change. This is the safest approach.
Next: Learn about Indexes.