LESSON 17 / 20·2 min readIntermediate

What are hooks, virtuals, instance methods and static methods?

01 Understand02 Recall03 Prepare

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; save hooks 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 this is a document or a query; bulk and update operations may bypass save-oriented behavior.

PAUSE & RECALL

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.

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

  • Explain which operations trigger the behavior, especially when a security-sensitive transformation such as hashing is involved.
Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Is Mongoose validation enough to protect data?

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.