mongodb로 댓글 대댓글 구현하기

조회수 882회

몽고디비로 댓글 대댓글 을 구현하려고 합니다.

스키마는

const replySchema = new Schema({
  comment_id: Number,
  creation_time: Number,
  post_id: Number,
  user: {
    user_id: String,
    loginType: String,
    user_profile_img: String,
    nick_name: String,
  },
  comment: String,
  parents: Number,
});
const userSchema = new Schema({
  user_id: String,
  loginType: String,
  user_profile_img: String,
  nick_name: String,
});
const commentSchema = new Schema({
  creation_time: Number,
  post_id: Number,
  user: userSchema,
  comment: String,
  children: [replySchema],
});

이렇게 되어 있습니다. 저게 comment라는 모델로 정의가 되어있는데 reply라는 모델로 따로 빼서 하는게 맞는 건지 아니면 저렇게 하나로 두고 하는 게 맞는건지 모르겠습니다. 댓글 추가는

const comment = new commentModel();

이렇게 모델을 가져와(?)

comment.create ({
---
})

이렇게 쓴다고 하면 대댓글은 코드를 어떻게 작성해야할까요..?

  • 제가 mongodb는 모르지만 일반적으로 말해서 대댓글이란 댓글의 일종으로 취급하시면 됩니다. 즉, 게시물을 부모로 갖는 댓글이 그냥 댓글이고, 다른 댓글을 부모로 갖는 댓글이 대댓글이 됩니다. 엽토군 2020.1.2 15:09

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)