“Mongoose virtual” Kode Jawaban

Mongoose virtual

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

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

virtual lumpur

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

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

Jawaban yang mirip dengan “Mongoose virtual”

Pertanyaan yang mirip dengan “Mongoose virtual”

Lebih banyak jawaban terkait untuk “Mongoose virtual” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya