title: How to push an element to an array in a MongoDB document on Nest.js?
body: <p>I feel extremely dumb making this question, but it's my first time working with Typescript in general and Nest.js in particular. I'm working with Nest, MongoDB (through Mongoose) and Express.js.</p>
  
  <p>I have two models: User and Post. For this, the relevant one would be the User:</p>
  
  <pre class="lang-js prettyprint-override"><code>import * as Mongoose from 'mongoose';
  
  export const UserSchema = new Mongoose.Schema(
    {
      username: { type: String, required: true },
      posts: { type: [Mongoose.SchemaTypes.ObjectId], ref: 'Post' },
      favs: { type: [Mongoose.SchemaTypes.ObjectId], ref: 'Post' },
    },
    {
      timestamps: true,
    },
  );
  </code></pre>
  
  <p>I'm making an API for a Twitter-like app, in which an user can create posts and add them to Favorites. I'm following tutorials, as one does when new to a technology, but I'm struggling to see how to push a new fav to the user's favs. Here's the User controller, so far:</p>
  
  <pre class="lang-js prettyprint-override"><code>import {
    Controller,
    Get,
    Req,
    Res,
    HttpStatus,
    Put,
    NotFoundException,
    Param,
  } from '@nestjs/common';
  import { UserService } from './user.service';
  import { Request, Response } from 'express';
  import { CreateUserDTO } from './dto/create-user.dto';
  
  @Controller('user')
  export class UserController {
    constructor(private userService: UserService) {}
  
    //fetch an user
    @Get('userfavs/:userID')
    async getCustomer(@Res() res: Response, @Param('userID') userID: string) {
      const user = await this.userService.getUser(userID);
  
      if (!user) throw new NotFoundException('This user does not exist!');
  
      return res.status(HttpStatus.OK).json({
        username: user.username,
  
        favs: user.favs,
      });
    }
  
    @Put('addfav/:favID')
    async updateUser(
      @Req() req: Request,
      @Res() res: Response,
      @Param('favID') favID: string,
      @Body() createUserDTO: CreateUserDTO,
    ) {
      const user = await this.userService.updateUser(req.user._id, createUserDTO);
      if (!user) throw new NotFoundException('This user does not exist!');
      return res.status(HttpStatus.OK).json({
        message: 'Fav added successfully!',
      });
    }
  }
  </code></pre>
  
  <p>And the service: </p>
  
  <pre class="lang-js prettyprint-override"><code>import { Injectable } from '@nestjs/common';
  import { Model } from 'mongoose';
  import { InjectModel } from '@nestjs/mongoose';
  import { User } from './interfaces/user.interface';
  import { CreateUserDTO } from './dto/create-user.dto';
  
  @Injectable()
  export class UserService {
    //creates Mongoose model for the User
    constructor(@InjectModel('User') private readonly userModel: Model&lt;User&gt;) {}
  
    //fetch a specific user - this will be useful to check favs
    async getUser(userID: string): Promise&lt;User&gt; {
      const user = await this.userModel
        .findById(userID)
        .populate('favs')
        .exec();
      return user;
    }
  
    //edit an specific user
    async updateUser(
      userID: string,
      createUserDTO: CreateUserDTO,
    ): Promise&lt;User&gt; {
      const updatedUser = await this.userModel.findByIdAndUpdate(
        userID,
        createUserDTO,
        { new: true },
      );
      return updatedUser;
    }
  }
  </code></pre>
  
  <p>In Node.js I would have done like:</p>
  
  <pre class="lang-js prettyprint-override"><code>User.findByIdAndUpdate(req.user._id, {
          $push: { favs: favId },
        })
  </code></pre>
  
  <p>But in Nest, the DTO is throwing me for a loop.</p>