Andrew Kim
Andrew Kim's Blog

Andrew Kim's Blog

How to add virtual function to Mongo Schema in NestJS

Andrew Kim's photo
Andrew Kim
·Nov 19, 2021·

1 min read

The title is a mouthful but it is what it is. Just wanted to drop a short memo.

I was trying to figure out how to get the right format and here is the code. After defining the schema, at the end, you can add a virtual function like so.

 ExamMetaSchema.virtual('fullname').get(function () {
   return `${this.firstname} ${this.lastname}`;
 });
 
Share this