How do I create a good Schema for a forum I build and how to use subdocuments the right way?











up vote
0
down vote

favorite












question about mongoose.



So I want to build a forum. I have written down a note how I want to structure it and it looks like this (note that mongoose.id is not an actual thing, it's just like a comment):



const topicGroupSchema = {
_id: mongoose.id,
title: String,
topics: [topicSchema]
};

const topicSchema = {
_id: mongoose.id,
title: String,
topicGroup: topicGroupSchema,
description: String,
latestTopic: String,
posts: [postSchema]
};

const postSchema = {
_id: mongoose.id,
title: String,
body: String,
author: String,
topic: topicSchema,
createdAt: { type: Date, default: Date.now },
replies: [replySchema]
};

const replySchema = {
_id: mongoose.id,
replier: String,
createdAt: { type: Date, default: Date.now },
text: String
};


Am I doing this right? So the idea is this:




  • In a forum there are many topicGroups (like "languages")


  • One topicGroup can have multiple topic's (like "javascript, "C#")


  • One topic can have multiple posts (like "How can I get this code to run")


  • One post can have multiple replies (like "have you debugged it yet?")



So now let's say a new post was made in a forum (topic).



I would create this post like this:



const newPost = {

title: "new post",

body: "my text",

author: "nice guy",

topic: // what to put in here??? I just want to relate it to the topic it was
created at, or can I get rid of it entirely?,

replies:

}


So the key question is here, when I already have a few topicGroups and a few topics, and inside one of such topic, I create a new post, what do I put in "topic" key of the post Schema? An object of that topic? Or do I query for this specific topic and push a new post into it's posts subdoc?
Why do I need the topic key in the post Schema, or do I need that?



I think I might need it because when I do, I can query a specific post, and ask mongoose to which topic it's related to, right?



Am I doing that completely wrong or what is there to know? Of course I read through all the mongoose docs, but it's still not clear to me.










share|improve this question




























    up vote
    0
    down vote

    favorite












    question about mongoose.



    So I want to build a forum. I have written down a note how I want to structure it and it looks like this (note that mongoose.id is not an actual thing, it's just like a comment):



    const topicGroupSchema = {
    _id: mongoose.id,
    title: String,
    topics: [topicSchema]
    };

    const topicSchema = {
    _id: mongoose.id,
    title: String,
    topicGroup: topicGroupSchema,
    description: String,
    latestTopic: String,
    posts: [postSchema]
    };

    const postSchema = {
    _id: mongoose.id,
    title: String,
    body: String,
    author: String,
    topic: topicSchema,
    createdAt: { type: Date, default: Date.now },
    replies: [replySchema]
    };

    const replySchema = {
    _id: mongoose.id,
    replier: String,
    createdAt: { type: Date, default: Date.now },
    text: String
    };


    Am I doing this right? So the idea is this:




    • In a forum there are many topicGroups (like "languages")


    • One topicGroup can have multiple topic's (like "javascript, "C#")


    • One topic can have multiple posts (like "How can I get this code to run")


    • One post can have multiple replies (like "have you debugged it yet?")



    So now let's say a new post was made in a forum (topic).



    I would create this post like this:



    const newPost = {

    title: "new post",

    body: "my text",

    author: "nice guy",

    topic: // what to put in here??? I just want to relate it to the topic it was
    created at, or can I get rid of it entirely?,

    replies:

    }


    So the key question is here, when I already have a few topicGroups and a few topics, and inside one of such topic, I create a new post, what do I put in "topic" key of the post Schema? An object of that topic? Or do I query for this specific topic and push a new post into it's posts subdoc?
    Why do I need the topic key in the post Schema, or do I need that?



    I think I might need it because when I do, I can query a specific post, and ask mongoose to which topic it's related to, right?



    Am I doing that completely wrong or what is there to know? Of course I read through all the mongoose docs, but it's still not clear to me.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      question about mongoose.



      So I want to build a forum. I have written down a note how I want to structure it and it looks like this (note that mongoose.id is not an actual thing, it's just like a comment):



      const topicGroupSchema = {
      _id: mongoose.id,
      title: String,
      topics: [topicSchema]
      };

      const topicSchema = {
      _id: mongoose.id,
      title: String,
      topicGroup: topicGroupSchema,
      description: String,
      latestTopic: String,
      posts: [postSchema]
      };

      const postSchema = {
      _id: mongoose.id,
      title: String,
      body: String,
      author: String,
      topic: topicSchema,
      createdAt: { type: Date, default: Date.now },
      replies: [replySchema]
      };

      const replySchema = {
      _id: mongoose.id,
      replier: String,
      createdAt: { type: Date, default: Date.now },
      text: String
      };


      Am I doing this right? So the idea is this:




      • In a forum there are many topicGroups (like "languages")


      • One topicGroup can have multiple topic's (like "javascript, "C#")


      • One topic can have multiple posts (like "How can I get this code to run")


      • One post can have multiple replies (like "have you debugged it yet?")



      So now let's say a new post was made in a forum (topic).



      I would create this post like this:



      const newPost = {

      title: "new post",

      body: "my text",

      author: "nice guy",

      topic: // what to put in here??? I just want to relate it to the topic it was
      created at, or can I get rid of it entirely?,

      replies:

      }


      So the key question is here, when I already have a few topicGroups and a few topics, and inside one of such topic, I create a new post, what do I put in "topic" key of the post Schema? An object of that topic? Or do I query for this specific topic and push a new post into it's posts subdoc?
      Why do I need the topic key in the post Schema, or do I need that?



      I think I might need it because when I do, I can query a specific post, and ask mongoose to which topic it's related to, right?



      Am I doing that completely wrong or what is there to know? Of course I read through all the mongoose docs, but it's still not clear to me.










      share|improve this question















      question about mongoose.



      So I want to build a forum. I have written down a note how I want to structure it and it looks like this (note that mongoose.id is not an actual thing, it's just like a comment):



      const topicGroupSchema = {
      _id: mongoose.id,
      title: String,
      topics: [topicSchema]
      };

      const topicSchema = {
      _id: mongoose.id,
      title: String,
      topicGroup: topicGroupSchema,
      description: String,
      latestTopic: String,
      posts: [postSchema]
      };

      const postSchema = {
      _id: mongoose.id,
      title: String,
      body: String,
      author: String,
      topic: topicSchema,
      createdAt: { type: Date, default: Date.now },
      replies: [replySchema]
      };

      const replySchema = {
      _id: mongoose.id,
      replier: String,
      createdAt: { type: Date, default: Date.now },
      text: String
      };


      Am I doing this right? So the idea is this:




      • In a forum there are many topicGroups (like "languages")


      • One topicGroup can have multiple topic's (like "javascript, "C#")


      • One topic can have multiple posts (like "How can I get this code to run")


      • One post can have multiple replies (like "have you debugged it yet?")



      So now let's say a new post was made in a forum (topic).



      I would create this post like this:



      const newPost = {

      title: "new post",

      body: "my text",

      author: "nice guy",

      topic: // what to put in here??? I just want to relate it to the topic it was
      created at, or can I get rid of it entirely?,

      replies:

      }


      So the key question is here, when I already have a few topicGroups and a few topics, and inside one of such topic, I create a new post, what do I put in "topic" key of the post Schema? An object of that topic? Or do I query for this specific topic and push a new post into it's posts subdoc?
      Why do I need the topic key in the post Schema, or do I need that?



      I think I might need it because when I do, I can query a specific post, and ask mongoose to which topic it's related to, right?



      Am I doing that completely wrong or what is there to know? Of course I read through all the mongoose docs, but it's still not clear to me.







      node.js mongodb mongoose






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 16:24

























      asked Nov 20 at 16:15









      GeraltDieSocke

      14211




      14211





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397161%2fhow-do-i-create-a-good-schema-for-a-forum-i-build-and-how-to-use-subdocuments-th%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397161%2fhow-do-i-create-a-good-schema-for-a-forum-i-build-and-how-to-use-subdocuments-th%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen