问题描述:

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 something like is in mongoose?

网友答案:

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';
  }
});
相关阅读:
Top