Skip to content Skip to sidebar Skip to footer

Does Mongoose Support Virtual Fields In Select Like Sql

In SQL I can make the following SELECT statement with the 'status' virtual field: SELECT CASE WHEN field = 1 THEN 'sale' ELSE 'none' END as status Does somethi

Solution 1:

Yes. Mongoose schemas support virtuals. Have a look at the schema section of the guide. I think you may want something like this:

var salesSchema = new Schema({
  sale: Number
});

salesSchema.virtual('status').get(function() {
  if (this.sale === 1) {
    return 'sale';
  } else {
    return 'none';
  }
});

Post a Comment for "Does Mongoose Support Virtual Fields In Select Like Sql"