Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongoose virtuals

//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

mongoose virtual

userSchema.virtual('id').get(function () {
    return this._id.toHexString();
})

userSchema.set('toJson', {virtual: true})
Comment

PREVIOUS NEXT
Code Example
Javascript :: import applymiddleware 
Javascript :: js map add property 
Javascript :: copy to clipboard javascript dom 
Javascript :: invisible character javascript 
Javascript :: javascript round to 1 decimal 
Javascript :: sort array without changing original array 
Javascript :: generate random number between two numbers javascript 
Javascript :: jquery reload iframe 
Javascript :: javascript get html slider value 
Javascript :: date format in react js 
Javascript :: can i pass data with usenavigate react router 
Javascript :: js text word wrap 
Javascript :: mysql json array contains 
Javascript :: remove duplicates from array 
Javascript :: today date javascript 
Javascript :: reload page angular one time 
Javascript :: js remove item array 
Javascript :: sort object by key value js 
Javascript :: owl carousel get started 
Javascript :: object length javascript 
Javascript :: update many mongoose 
Javascript :: random rgba color javascript except black 
Javascript :: decode morse code js 
Javascript :: jquery get body 
Javascript :: string contains substring javascript 
Javascript :: moment time ago format reactjs 
Javascript :: check if js property exists in class 
Javascript :: snentence case capitalisation js 
Javascript :: onclick inline function react 
Javascript :: sort object dictionary javscript 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =