• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

221
Views
What is a virtual for an URL in Mongoose?

I am currently following a tutorial on implementing a database (Mongoose) in a simple website created using Express-framework. I do not have any problem understanding the concept of models, but I fail to make sense of the lines following the comment "Virtual for book's URL" in the code attached. How do these lines operate, and what role does having a virtual property have in this context?

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var BookSchema = new Schema(
  {
    title: {type: String, required: true},
    author: {type: Schema.Types.ObjectId, ref: 'Author', required: true},
    summary: {type: String, required: true},
    isbn: {type: String, required: true},
    genre: [{type: Schema.Types.ObjectId, ref: 'Genre'}]
  }
);

// Virtual for book's URL
BookSchema
.virtual('url')
.get(function () {
  return '/catalog/book/' + this._id;
});

//Export model
module.exports = mongoose.model('Book', BookSchema);
about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Virtuals are typically used for computed properties on documents.

BookSchema
// 'url' is the name of the virtual
.virtual('url')
.get(function () {
 // you are computing dynamic url
  return '/catalog/book/' + this._id;
});

Important thing about virtuals is it does not store anything on database. Imagine you stored "name" and "lastname" but you want to display fullname without having "fullname" column. You would be using repeated data on database if you add fullname property to your schema, this will slow down and will also cost you. Instead you write virtual:

YourSchema.get(function(){
  return this.firstname + ' ' + this.lastname
}) 

You can also go one step further and return fullname property as it is in a column in database. mongoose Schema takes options object

let options={
   toJSON:{
       virtuals: true 
   }
}

set the virtual

  let YourSchemaVirtual=YourSchema.virtual('fullname')

now when querying the database, get the collection

 YourModel.findOne({firstname:"name"}).exec(function(err,user){
    res.send(user)
 })

In return value you will see fullname field computed as this.firstname + ' ' + this.lastname even though you have no fullname stored in database.

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error