라벨이 Collection인 게시물 표시

[MongoDB] Mongoose(몽구스) populate 사용해서 다른 collection의 documents ObjectId 참조하기

이미지
Mongoose(몽구스)에서 populate을 사용하면 다른 Collection에 있는 documents의 ObjectId를 이용하여 documents 참조가 가능하다. 조금 더 보충하자면 MongoDB 자체에서 Documents 마다 자동으로 만들어주는 고유값인 ObjectId가 있는데 다른 Collection에서 populate 메서드를 사용해서 하나의 Documents에 또 다른 Documents의 정보들을 같이 담을 수 있게 해준다. example) User.js (Model) const mongoose = require ( " mongoose " ) ; const bcrypt = require ( " bcrypt " ) ; const saltRounds = 10 ; const jwt = require ( " jsonwebtoken " ) ; const userSchema = mongoose . Schema ( {      email : {      type : String ,      unique : 1 ,      },      name : {      type : String ,      },      nickname : {      type : String ,      unique : 1 ,      },      password : {      type : String ,      minlength : 6 ,      },      eventAgreement : {      type : Boolean ,      },      createdAt : {      type : Number ,      },      google : {      type : String ,      },      github : {