Mongoose Virtual Properties Explained | Add Dynamic Fields Without Saving to DB | Part #123
🚀 Introduction: Have you ever needed a field in your API response that doesn't actually exist in your database? Maybe an "Age" field derived from a "Date of...

procademy
114 views • Apr 7, 2026

About this video
🚀 Introduction:
Have you ever needed a field in your API response that doesn't actually exist in your database? Maybe an "Age" field derived from a "Date of Birth," or an "IsPremium" flag based on price? In this lecture, we explore Mongoose Virtual Properties—a powerful feature that lets you define document properties that are computed dynamically but never persisted to MongoDB.
🤔 What you will learn in this video?
✔️ Concept of Virtuals: Understand why we use fields that exist in our code but not in our database.
✔️ Defining a Virtual: Learn how to use the .virtual() method on a Mongoose schema to create a custom getter.
✔️ Regular vs. Arrow Functions: Why you must use regular functions to access the this keyword (pointing to the current document).
✔️ JSON & Object Visibility: How to use the toJSON and toObject options to make your virtuals visible in Postman and your app logic.
✔️ Querying Limitations: A critical look at why you cannot use virtual properties inside a .find() filter.
🗒️ Virtual Properties Cheat Sheet
Virtuals are perfect for data transformation and business logic that shouldn't clutter your database storage.
✅ The Getter Method: Use .get(function() { ... }) to define the logic for your virtual field.
✅ The this Keyword: Inside the getter, this refers to the document instance, allowing access to other persisted fields.
✅ Schema Options: To see virtuals in your API, you must set { virtuals: true } in both toJSON and toObject settings.
✅ Performance: Since they are computed on the fly, virtuals keep your database lean but require CPU cycles during retrieval.
⏱️ Timelines:
0:00 - What are Virtual Properties?
1:16 - Real-world Examples (Age, Unit Conversions)
2:16 - Adding an isPremium Virtual to the Hotel Schema
3:40 - The Importance of Regular Functions vs. Arrow Functions
5:52 - Why Virtuals are Hidden by Default
7:12 - Enabling Virtuals for JSON (Postman Results)
8:40 - Enabling Virtuals for Objects (Application Logic)
10:17 - CRITICAL: Why you cannot Query by Virtual Properties
12:00 - Summary and Key Takeaways
👍 Level up your backend skills! Like and Subscribe to Procademy Classes for more advanced Mongoose tutorials! #NodeJS #MongoDB #Mongoose #WebDevelopment #BackendDevelopment #ProcademyClasses
Have you ever needed a field in your API response that doesn't actually exist in your database? Maybe an "Age" field derived from a "Date of Birth," or an "IsPremium" flag based on price? In this lecture, we explore Mongoose Virtual Properties—a powerful feature that lets you define document properties that are computed dynamically but never persisted to MongoDB.
🤔 What you will learn in this video?
✔️ Concept of Virtuals: Understand why we use fields that exist in our code but not in our database.
✔️ Defining a Virtual: Learn how to use the .virtual() method on a Mongoose schema to create a custom getter.
✔️ Regular vs. Arrow Functions: Why you must use regular functions to access the this keyword (pointing to the current document).
✔️ JSON & Object Visibility: How to use the toJSON and toObject options to make your virtuals visible in Postman and your app logic.
✔️ Querying Limitations: A critical look at why you cannot use virtual properties inside a .find() filter.
🗒️ Virtual Properties Cheat Sheet
Virtuals are perfect for data transformation and business logic that shouldn't clutter your database storage.
✅ The Getter Method: Use .get(function() { ... }) to define the logic for your virtual field.
✅ The this Keyword: Inside the getter, this refers to the document instance, allowing access to other persisted fields.
✅ Schema Options: To see virtuals in your API, you must set { virtuals: true } in both toJSON and toObject settings.
✅ Performance: Since they are computed on the fly, virtuals keep your database lean but require CPU cycles during retrieval.
⏱️ Timelines:
0:00 - What are Virtual Properties?
1:16 - Real-world Examples (Age, Unit Conversions)
2:16 - Adding an isPremium Virtual to the Hotel Schema
3:40 - The Importance of Regular Functions vs. Arrow Functions
5:52 - Why Virtuals are Hidden by Default
7:12 - Enabling Virtuals for JSON (Postman Results)
8:40 - Enabling Virtuals for Objects (Application Logic)
10:17 - CRITICAL: Why you cannot Query by Virtual Properties
12:00 - Summary and Key Takeaways
👍 Level up your backend skills! Like and Subscribe to Procademy Classes for more advanced Mongoose tutorials! #NodeJS #MongoDB #Mongoose #WebDevelopment #BackendDevelopment #ProcademyClasses
Tags and Topics
Browse our collection to discover more content in these categories.
Video Information
Views
114
Likes
4
Duration
12:23
Published
Apr 7, 2026
Related Trending Topics
LIVE TRENDSRelated trending topics. Click any trend to explore more videos.