What are hooks, virtuals, instance methods and static methods?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn What are hooks, virtuals, instance methods and static methods?
Definition:
Hooks run around selected document or query operations, virtuals compute values that are not normally stored, and methods attach reusable behavior to documents or models. These features keep related logic near the data model. However, each feature has a defined execution context and does not apply to every write path automatically.
Key points:
-
Hooks run around supported operations;
savehooks do not cover every update API. -
Virtuals are derived fields that are not stored in MongoDB.
-
Instance methods belong to a document; static methods belong to the model.
-
Know whether a hook's
thisis a document or a query; bulk and update operations may bypass save-oriented behavior.
Interview point:
- Explain which operations trigger the behavior, especially when a security-sensitive transformation such as hashing is involved.
Adapted from 04_MONGODB_MONGOOSE.md · “What are hooks, virtuals, instance methods and static methods?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.