定义了指定为virtual属性的post_count字段统计文章数
1 | // model |
使用find查询的时候居然查不出post_count出来。查看手册后是因为document 对象的toJSON和toObject方法里面 默认将virtual 属性排除了。 原文是这样的:
Note that if the resulting record is converted to an object or JSON, virtuals are not included by default. Pass virtuals : true to either toObject() or to toJSON() to have them returned.
需要对Schama做下面的配置:
1 | TagSchema.set('toJSON', { virtuals: true }); |