저번 시간에 Video model을 생성했었는데요. 유튜브를 생각하면 video가 있으면 video에 댓글도 달리겠죠. 이번 시간에는 Comment Model을 만들어보고 Model간의 관계를 정의해주는 relationship과정도 진행하겠습니다.
youtube
  |models
   +|Comment.js
 *|init.js
import mongoose from 'mongoose';
const {Schema} = mongoose;
const CommentSchema = new Schema({
    text:{
        type: String,
        required: 'Text is required'
    },
    createdAt:{
        type: Date,
        default: Date.now
    }
});
import './models/Comment';
이제 relationship을 정의해보겠습니다. video와 comment 사이에는 분명 어떤 relationship이 있습니다. relationship을 정해주는 방법이 두가지를 소개합니다.
const VideoSchema = new Schema({
  /*생략*/
  comments:[{
    type: mongoose.Schema.Types.ObjectId,
    //ref는 reference를 의미합니다.
    ref: 'Comment'
  }]
});
const CommentSchema = new Schema({
  /*생략*/
  video:{
  	type: Schema.Types.ObjectId,
    	ref: 'Video'
  }
});