Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
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 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda