몽구스에서 특정필드만 조회

Siwoo Pak·2021년 12월 21일
0

MongoDB

목록 보기
3/4

특정필드만 조회하기

  • 예제
// 유저 정보 조회(유저아이디, 이메일, 유저이름) - 마아페이지
  async getUserInfoById(id: string) {
    const user: any = await this.userModel
      .findById(id)
      .select("userId, email, userName");
    if (!user) throw new HttpError(404, "Not Found UserInfo");
    return user;
  }
  • select()를 써서 해당하는 필드만 조회해서 넘겨줄 수 있음
  • 저 코드에서 findById(id, "userId, email, userName") 형식으로도
    특정필드만 조회하기 가능
profile
'하루를 참고 인내하면 열흘을 벌 수 있고 사흘을 참고 견디면 30일을, 30일을 견디면 3년을 벌 수 있다.'

0개의 댓글