Mongo DB design with nested document












0















I would like best practices advice concerning MongoDB schema design.
I am creating a project manager. I want users to be able to create tasks and this task to be associated with a phase of the project.
I have 2 designs in mind with both pros and cons:



The first design is:



const ProjectSchema: new Schema ({
...
phases: [{
phase_id: String,
name: String,
status: String,
startDate: Date,
endDate: Date,
tasks: [{
task_id: String,
title: String,
description: String,
completed: { type: Boolean, default: 'false' },
...
}],
}]
...
});


Here the advantage is that I have tasks per phase so if want all the tasks of a phase the query will take less time to process as I won't have to filter everytime I want tasks of a certain phase. However, if I need to query all the tasks of the project I need to get all the phases and then get the tasks which make 2 queries.



The other design is:



const ProjectScema: new Schema ({
...
phases: [{
phase_id: String,
name: String,
status: String,
startDate: Date,
endDate: Date,
...
}],
tasks: [{
task_id: String,
title: String,
description: String,
completed: { type: Boolean, default: 'false' },
phase: { id: mongoose.Types.ObjectId, name: String },
...
}],
...
});


In this case, it is easier to get all the tasks that are assigned in a project but if I want all the tasks of one phase I'll have to filter the array of tasks.



In both cases, if I have a consequent number of tasks the queries may be slow. Is there a better design that I can use?



Another solution would be to have the tasks in another collection and having a reference to the tasks in the phases:



const TaskSchema: new Schema({
task_id: String
title: String,
description: String,
startDate: Date,
endDate: Date,
project_id: String,
phase_id: String,
...
});

TaskSchema.index({ task_id: 1 });
TaskSchema.index({ project_id: 1 });
const Task = mongoose.model('Task', TaskSchema);


And in project shcema:



const ProjectScema: new Schema ({
project_id: String,
...
phases: [{
phase_id: String,
name: String,
status: String,
startDate: Date,
endDate: Date,
tasks: [{
type: Schema.Types.ObjectId,
ref: 'Task'
}],
...
}]
...
});


This way I can query all the tasks of a project thanks to the index or get all the tasks of a phase thanks to populating.
I don't know which solution is best to use knowing that I will read and write tasks and phases quite often and that it's possible to have a large number of phases and tasks (1-2 hundred).










share|improve this question





























    0















    I would like best practices advice concerning MongoDB schema design.
    I am creating a project manager. I want users to be able to create tasks and this task to be associated with a phase of the project.
    I have 2 designs in mind with both pros and cons:



    The first design is:



    const ProjectSchema: new Schema ({
    ...
    phases: [{
    phase_id: String,
    name: String,
    status: String,
    startDate: Date,
    endDate: Date,
    tasks: [{
    task_id: String,
    title: String,
    description: String,
    completed: { type: Boolean, default: 'false' },
    ...
    }],
    }]
    ...
    });


    Here the advantage is that I have tasks per phase so if want all the tasks of a phase the query will take less time to process as I won't have to filter everytime I want tasks of a certain phase. However, if I need to query all the tasks of the project I need to get all the phases and then get the tasks which make 2 queries.



    The other design is:



    const ProjectScema: new Schema ({
    ...
    phases: [{
    phase_id: String,
    name: String,
    status: String,
    startDate: Date,
    endDate: Date,
    ...
    }],
    tasks: [{
    task_id: String,
    title: String,
    description: String,
    completed: { type: Boolean, default: 'false' },
    phase: { id: mongoose.Types.ObjectId, name: String },
    ...
    }],
    ...
    });


    In this case, it is easier to get all the tasks that are assigned in a project but if I want all the tasks of one phase I'll have to filter the array of tasks.



    In both cases, if I have a consequent number of tasks the queries may be slow. Is there a better design that I can use?



    Another solution would be to have the tasks in another collection and having a reference to the tasks in the phases:



    const TaskSchema: new Schema({
    task_id: String
    title: String,
    description: String,
    startDate: Date,
    endDate: Date,
    project_id: String,
    phase_id: String,
    ...
    });

    TaskSchema.index({ task_id: 1 });
    TaskSchema.index({ project_id: 1 });
    const Task = mongoose.model('Task', TaskSchema);


    And in project shcema:



    const ProjectScema: new Schema ({
    project_id: String,
    ...
    phases: [{
    phase_id: String,
    name: String,
    status: String,
    startDate: Date,
    endDate: Date,
    tasks: [{
    type: Schema.Types.ObjectId,
    ref: 'Task'
    }],
    ...
    }]
    ...
    });


    This way I can query all the tasks of a project thanks to the index or get all the tasks of a phase thanks to populating.
    I don't know which solution is best to use knowing that I will read and write tasks and phases quite often and that it's possible to have a large number of phases and tasks (1-2 hundred).










    share|improve this question



























      0












      0








      0








      I would like best practices advice concerning MongoDB schema design.
      I am creating a project manager. I want users to be able to create tasks and this task to be associated with a phase of the project.
      I have 2 designs in mind with both pros and cons:



      The first design is:



      const ProjectSchema: new Schema ({
      ...
      phases: [{
      phase_id: String,
      name: String,
      status: String,
      startDate: Date,
      endDate: Date,
      tasks: [{
      task_id: String,
      title: String,
      description: String,
      completed: { type: Boolean, default: 'false' },
      ...
      }],
      }]
      ...
      });


      Here the advantage is that I have tasks per phase so if want all the tasks of a phase the query will take less time to process as I won't have to filter everytime I want tasks of a certain phase. However, if I need to query all the tasks of the project I need to get all the phases and then get the tasks which make 2 queries.



      The other design is:



      const ProjectScema: new Schema ({
      ...
      phases: [{
      phase_id: String,
      name: String,
      status: String,
      startDate: Date,
      endDate: Date,
      ...
      }],
      tasks: [{
      task_id: String,
      title: String,
      description: String,
      completed: { type: Boolean, default: 'false' },
      phase: { id: mongoose.Types.ObjectId, name: String },
      ...
      }],
      ...
      });


      In this case, it is easier to get all the tasks that are assigned in a project but if I want all the tasks of one phase I'll have to filter the array of tasks.



      In both cases, if I have a consequent number of tasks the queries may be slow. Is there a better design that I can use?



      Another solution would be to have the tasks in another collection and having a reference to the tasks in the phases:



      const TaskSchema: new Schema({
      task_id: String
      title: String,
      description: String,
      startDate: Date,
      endDate: Date,
      project_id: String,
      phase_id: String,
      ...
      });

      TaskSchema.index({ task_id: 1 });
      TaskSchema.index({ project_id: 1 });
      const Task = mongoose.model('Task', TaskSchema);


      And in project shcema:



      const ProjectScema: new Schema ({
      project_id: String,
      ...
      phases: [{
      phase_id: String,
      name: String,
      status: String,
      startDate: Date,
      endDate: Date,
      tasks: [{
      type: Schema.Types.ObjectId,
      ref: 'Task'
      }],
      ...
      }]
      ...
      });


      This way I can query all the tasks of a project thanks to the index or get all the tasks of a phase thanks to populating.
      I don't know which solution is best to use knowing that I will read and write tasks and phases quite often and that it's possible to have a large number of phases and tasks (1-2 hundred).










      share|improve this question
















      I would like best practices advice concerning MongoDB schema design.
      I am creating a project manager. I want users to be able to create tasks and this task to be associated with a phase of the project.
      I have 2 designs in mind with both pros and cons:



      The first design is:



      const ProjectSchema: new Schema ({
      ...
      phases: [{
      phase_id: String,
      name: String,
      status: String,
      startDate: Date,
      endDate: Date,
      tasks: [{
      task_id: String,
      title: String,
      description: String,
      completed: { type: Boolean, default: 'false' },
      ...
      }],
      }]
      ...
      });


      Here the advantage is that I have tasks per phase so if want all the tasks of a phase the query will take less time to process as I won't have to filter everytime I want tasks of a certain phase. However, if I need to query all the tasks of the project I need to get all the phases and then get the tasks which make 2 queries.



      The other design is:



      const ProjectScema: new Schema ({
      ...
      phases: [{
      phase_id: String,
      name: String,
      status: String,
      startDate: Date,
      endDate: Date,
      ...
      }],
      tasks: [{
      task_id: String,
      title: String,
      description: String,
      completed: { type: Boolean, default: 'false' },
      phase: { id: mongoose.Types.ObjectId, name: String },
      ...
      }],
      ...
      });


      In this case, it is easier to get all the tasks that are assigned in a project but if I want all the tasks of one phase I'll have to filter the array of tasks.



      In both cases, if I have a consequent number of tasks the queries may be slow. Is there a better design that I can use?



      Another solution would be to have the tasks in another collection and having a reference to the tasks in the phases:



      const TaskSchema: new Schema({
      task_id: String
      title: String,
      description: String,
      startDate: Date,
      endDate: Date,
      project_id: String,
      phase_id: String,
      ...
      });

      TaskSchema.index({ task_id: 1 });
      TaskSchema.index({ project_id: 1 });
      const Task = mongoose.model('Task', TaskSchema);


      And in project shcema:



      const ProjectScema: new Schema ({
      project_id: String,
      ...
      phases: [{
      phase_id: String,
      name: String,
      status: String,
      startDate: Date,
      endDate: Date,
      tasks: [{
      type: Schema.Types.ObjectId,
      ref: 'Task'
      }],
      ...
      }]
      ...
      });


      This way I can query all the tasks of a project thanks to the index or get all the tasks of a phase thanks to populating.
      I don't know which solution is best to use knowing that I will read and write tasks and phases quite often and that it's possible to have a large number of phases and tasks (1-2 hundred).







      database mongodb mongoose database-design mongodb-query






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 30 '18 at 14:01







      lvndry

















      asked Dec 28 '18 at 23:26









      lvndrylvndry

      439




      439
























          0






          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%2f53965371%2fmongo-db-design-with-nested-document%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53965371%2fmongo-db-design-with-nested-document%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

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas