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 :: javascript async function 
Javascript :: yagni 
Javascript :: react native location 
Javascript :: string to json nodejs 
Javascript :: react router dom private route 
Javascript :: join method javascript 
Javascript :: angular ng default scss 
Javascript :: react-file-base64 
Javascript :: next router 
Javascript :: js array add every element of array 
Javascript :: nodejs bodyparser form data 
Javascript :: counter in javascript 
Javascript :: js looping through array 
Javascript :: javascript object to array 
Javascript :: js compare values of two arrays 
Javascript :: nodejs delete s3 folder 
Javascript :: checkbox jquery checked 
Javascript :: @babel/plugin-proposal-optional-chaining 
Javascript :: generate an array of random numbers javascript 
Javascript :: adb reverse USB debugging 
Javascript :: node express app.listen at specific port & host 
Javascript :: .NET number values such as positive and negative infinity cannot be written as valid JSON. 
Javascript :: javascript is null 
Javascript :: node js post method 
Javascript :: discord.js v13 if dm 
Javascript :: check if number is decimal or integer js 
Javascript :: javascript html append 
Javascript :: deploy vue js to shared hosting 
Javascript :: create new angular project with specific version 
Javascript :: get option value jquery 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =