美文网首页
mongoose Virtuals

mongoose Virtuals

作者: 坠入莱茵河 | 来源:发表于2017-12-29 14:05 被阅读0次

Virtuals属性可以从Document中获取并设置 但它并不存在mongoDB中
Virtuals的getters能够有效的格式化以及合并文本域

personSchema.virtual('fullName').get(function () {
  return this.name.first + ' ' + this.name.last;
});

var Person = mongoose.model('Person', personSchema);

var axl = new Person({
  name: { first: 'Axl', last: 'Rose' }
});

axl.name.fullName // Axl Rose

setters能够有效的将数据库中的一个字段拆解为多个字段

当使用toJSON toObject JSON.stringify 默认将不包含 Virtuals 属性
Schema设置toJSON: { virtuals: true }时就会包含了

相关文章

网友评论

      本文标题:mongoose Virtuals

      本文链接:https://www.haomeiwen.com/subject/repigxtx.html