Mongodb – What would be better long term: Creating many documents in MongoDB or fewer documents with nested arrays

mongodb

I have a scenario I'm debating for my app.

Let's say I have a Quiz application with a MongoDB structure that looks like this:

{
_id: xxxxxx,
userId: xxxx,
quizData: [
{
name: xxxx,
quizId: xxxx, 
questions: [...]
},
{
name: xxxx,
quizId: xxxx, 
questions: [...]
}
...]}

I'm torn between what's better long-term for my application: let each user have just one document and for each quiz he/she creates, it will be added to the "quizData" array of objects…OR…let each user's quiz have its own document in the MongoDB, where I create the above structure for each quiz (obviously, the "quizData" will only contain one object instead of an array of many).

Theoretically, the user can create as many Quizzes as they want, but realistically, that probably won't be the case. I fear that if I let each user have it's own document for each quiz in the MongoDB, it will populate the collection fast and over time (with many users doing the same), it will slow down the querying process.

Anyone have any thoughts on the best way to go about this?

Best Answer

I'm torn between what's better long-term for my application: let each user have just one document and for each quiz he/she creates, it will be added to the "quizData" array of objects...OR...let each user's quiz have its own document in the MongoDB, where I create the above structure for each quiz (obviously, the "quizData" will only contain one object instead of an array of many).

Arrays which potentially grow without bound are an antipattern for performance and usability.

I would recommend having each quiz in a separate document for reasons including:

  • Better granularity for server memory management and document updates. When you retrieve or update a single quiz, the MongoDB server does not have to fetch/update a large document with all the quizzes for that user.
  • Easier to manipulate documents in your application code. Arrays of arrays are awkward to work with and have extra overhead if if you want to pull out a subset of elements via server-side manipulation. Similarly, if you wanted to provide a list of active/available quizzes in your application it would be easier to have quiz as a top-level object.
  • Avoid worrying about hitting the document size limit (16MB as at MongoDB 3.2). If users post many (or large) quizzes, a single document per user makes the document limit a much more likely issue to have to deal with in your application code.

For more information, see: Why shouldn't I embed large arrays in my documents?.