title: How to serialize a nest js response with class-transformer while getting data with Typegoose?
body: <p>I have been trying to work through the NestJs example for the Serialization Section for Mongodb using Typegoose using the class-transformer library. The example given at <a href="https://docs.nestjs.com/techniques/serialization" rel="noreferrer">https://docs.nestjs.com/techniques/serialization</a> only shows how to use serialization in TypeORM. I followed the same process for Typegoose. Here is what I have tried so far.</p>
  
  <pre><code>// cat.domain.ts
  
  import { prop } from '@typegoose/typegoose';
  
  export class Cat {
    @prop()
    name: string;
  
    @prop()
    age: number;
  
    @prop()
    breed: string;
  }
  
  
  // cats.service.ts
  
  @Injectable()
  export class CatsService {
    constructor(
      @InjectModel(Cat) private readonly catModel: ReturnModelType&lt;typeof Cat&gt;,
    ) {}
  
    findAll(): Observable&lt;Cat[]&gt; {
      return from(this.catModel.find().exec());
    }
  
    findOne(id: string): Observable&lt;Cat&gt; {
      return from(this.catModel.findById(id).exec());
    }
    ...
  }
  
  // cat.response.ts
  
  import { ObjectId } from 'mongodb';
  import { Exclude, Transform } from 'class-transformer';
  
  export class CatResponse {
    @Transform(value =&gt; value.toString(), { toPlainOnly: true })
    _id?: ObjectId;
  
    name: string;
  
    age: number;
  
    @Exclude()
    breed: string;
  
    constructor(partial: Partial&lt;CatResponse&gt;) {
      Object.assign(this, partial);
    }
  }
  
  // cats.controller.ts
  
  @Controller('cats')
  @UseInterceptors(ClassSerializerInterceptor)
  export class CatsController {
    constructor(private readonly catsService: CatsService) {}
  
    @Get()
    findAll(): Observable&lt;CatResponse[]&gt; {
      return this.catsService.findAll();
    }
  
    @Get(':id')
    findOne(@Param() params: FindOneParamsDto): Observable&lt;CatResponse&gt; {
      return this.catsService.findOne(params.id);
    }
    ...
  }
  </code></pre>
  
  <p>I tried running the API call on Get() with id but instead of the <code>breed</code> being excluded from the response I have been getting the following response.</p>
  
  <pre><code>{
      "$__": {
          "strictMode": true,
          "selected": {},
          "getters": {},
          "_id": {
              "_bsontype": "ObjectID",
              "id": {
                  "type": "Buffer",
                  "data": [
                      94,
                      93,
                      76,
                      66,
                      116,
                      204,
                      248,
                      112,
                      147,
                      216,
                      167,
                      205
                  ]
              }
          },
          "wasPopulated": false,
          "activePaths": {
              "paths": {
                  "_id": "init",
                  "name": "init",
                  "age": "init",
                  "breed": "init",
                  "__v": "init"
              },
              "states": {
                  "ignore": {},
                  "default": {},
                  "init": {
                      "_id": true,
                      "name": true,
                      "age": true,
                      "breed": true,
                      "__v": true
                  },
                  "modify": {},
                  "require": {}
              },
              "stateNames": [
                  "require",
                  "modify",
                  "init",
                  "default",
                  "ignore"
              ]
          },
          "pathsToScopes": {},
          "cachedRequired": {},
          "$setCalled": [],
          "emitter": {
              "_events": {},
              "_eventsCount": 0,
              "_maxListeners": 0
          },
          "$options": {
              "skipId": true,
              "isNew": false,
              "willInit": true
          }
      },
      "isNew": false,
      "_doc": {
          "_id": {
              "_bsontype": "ObjectID",
              "id": {
                  "type": "Buffer",
                  "data": [
                      94,
                      93,
                      76,
                      66,
                      116,
                      204,
                      248,
                      112,
                      147,
                      216,
                      167,
                      205
                  ]
              }
          },
          "name": "Sylver",
          "age": 14,
          "breed": "Persian Cat",
          "__v": 0
      },
      "$locals": {},
      "$op": null,
      "$init": true
  }
  </code></pre>
  
  <p>Can anyone help me with how to serialize response properly?</p>