Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

virtual properties in mongoose model

//virtual properties are fields that are not stored in the database
//but are computed from other fields.
//we can use them to add extra fields to our schema

//model/tourModel.js
tourSchema.virtual('durationWeeks').get(function() {
     //we use this.duration because we want to use this in getter
     return this.duration/7
})
const tourSchema= new mongoose.Schema({},{
     toJSON:{virtuals:true}, // in options object we can pass virtuals:true to get virtual properties in json
     toObject:{virtuals:true},
})

//we cann't use virtual properties in our query because they are not stored in the database
Comment

PREVIOUS NEXT
Code Example
Javascript :: semantics ui complete responsive menu 
Javascript :: sessionstorage array 
Javascript :: sum all elements in array javascript 
Javascript :: disable angular cache option 
Javascript :: get time from date 
Javascript :: first and last char vowel reg exp same char 
Javascript :: date and time in javascript 
Javascript :: Getting Binary gap in JS 
Javascript :: npm http status codes 
Javascript :: regex to ignore white spaces js 
Javascript :: dociql process.env.NODE_TLS_REJECT_UNAUTHORIZED=0 
Javascript :: generate random id javascript 
Javascript :: ReferenceError: primordials is not defined 
Javascript :: node js server get images from folder 
Javascript :: today date js 
Javascript :: how to disable back js 
Javascript :: get array index by key value js 
Javascript :: javascript sort array by index 
Javascript :: sequelize dialect 
Javascript :: select2 find option by value 
Javascript :: javascript window size 
Javascript :: react use same useState for multiple inputs 
Javascript :: moment check greater than current time 
Javascript :: javascript xor 
Javascript :: contains substring javascript 
Javascript :: file picker electron 
Javascript :: redirect to given link jquer 
Javascript :: chrome add a javascript bookmark 
Javascript :: TypeError: sequelize.import is not a function 
Javascript :: jquery remove option from select 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =