Search⌘ K
AI Features

Timestamps, Soft Deletes, and Audit Fields

Explore how to implement timestamps and soft delete patterns in MongoDB schemas to track creation, updates, and deletion states. Understand how Mongoose automates audit fields and why soft deletes are important for data recovery and audit trails. Learn best practices to filter deleted documents and maintain data integrity in production MERN applications.

Real applications need to track two things that a basic schema does not capture by default: when the document was created, when it was last updated, and whether it has been deleted. Audit fields track creation and update history, while soft deletes track deletion state. They reduce the risk of accidental data loss and provide an audit trail of what changed and when. These patterns appear throughout the rest of the course, so establish them now as a default practice, not an afterthought.

Note: The seeded e-commerce data is kept minimal and does not include these fields. They are schema features you add for production; the examples below show how Mongoose manages them automatically.

To make the additions concrete, the next visual should show a document gaining a timestamp and soft delete fields:

A product document extended with a timestamp and soft delete fields
A product document extended with a timestamp and soft delete fields

That starts with the timestamps Mongoose can manage for you.

Automatic timestamps with timestamps: true

Manually setting an updatedAt field on every write is easy to miss, and one missed update leaves the timestamp stale. Mongoose automates this with the timestamps schema option. Passing { ...