Charles
Charles

Reputation: 648

How to model data in MongoDB?

I am new to non relational databases hence I was wondering how might I model my data. My project is a simple todo app that has projects and tasks. Please see below for how I currently do it and let me know if it is correct or what I may do better:

projects: [{
   _id: 23423423423,
   name: projectName1,
   tasks: [{
               _id: 3423423423423324,
               title: "do something",
               isCompleted: "false"
          },
          {
               _id: 23223423423324,
               title: "do something else now",
               isCompleted: "false"
          },
          {
               _id: 232234233423423,
               title: "i have completed this",
               isCompleted: "true"
          }]
  },
  {
   _id: 2342123123,
   name: projectName2,
   tasks: [{
               _id: 3423423423422314,
               title: "build this",
               isCompleted: "false"
          },
          {
               _id: 2322342342234234,
               title: "build something else now",
               isCompleted: "false"
          },
          {
               _id: 2322342323423423,
               title: "i have completed building this",
               isCompleted: "true"
          }]
  }

Upvotes: 0

Views: 138

Answers (1)

Sammaye
Sammaye

Reputation: 43884

Assuming projects is actually the collection name then this looks good, if not then it might be more performant to make a document per project and have them link together using a user_id of some sort.

One thing you might want to bare in mind here is how many new tasks or updates to tasks you get in a certain period of time. One reason for this because if your subdocument tasks grows consistently with time you may face certain fragmentation problems ( http://www.10gen.com/presentations/storage-engine-internals can explain more ), if this become problematic (whihc I doubt very much it will) you could use power of 2 sizes allocation: http://docs.mongodb.org/manual/reference/command/collMod/#usePowerOf2Sizes

However, in general this seems about right.

Upvotes: 2

Related Questions