What are hooks, virtuals, instance methods and static methods?
Understand the concept
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.
Important ideas
-
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.
Can you explain it without the notes?
What are hooks, virtuals, instance methods and static methods?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
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.
Turn this concept into a clear answer.
- 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?”. Examples are study fragments; framework behavior can depend on your version.