当前位置: 动力学知识库 > 问答 > 编程问答 >

node.js - 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 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';
  }
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: