Virtual Properties Flashcards
1
Q
What is a virtual property?
A
It is a property that is returned with documents during a query but is not stored in the database
2
Q
How do you create a virtual property in Mongoose?
A
SchemaName.virtual(‘virtual property name’).get(function (){return this.attr * this.attr} ) ;
3
Q
How do you make Mongoose aware of virtual properties?
A
const SchemaName = new mongoose.Schema({}, { toJSON: {virtuals: true}, toObject: {virtuals: true} })