Home > Software design >  How should i structure my MongoDb database? A lot of small documents or embedded fewer documents?
How should i structure my MongoDb database? A lot of small documents or embedded fewer documents?

Time:09-26

I am new to python and very new to MongoDb. I made an application to store trivia questions, currently in a json. This is how the overall structure looks like:

enter image description here

This is an example of single answer questions, in art category:enter image description here

And this is an example of multiple choice questions in art category :

enter image description here

As you can see, in both cases i use the question itself as key and it's answer as value. So, to get the answer to a question i would just do: answers = dictionary["multiple"]["art"]["What is a sitar?"] and i would get :

["Instrument",
"Food",
"Insect",
"Temple"] 

My application runs as you would expect. When i get a new question, i know it's subject (art, biology, etc). If question doesn't exist i just add it in the right category.

I want to move all my saved questions and answers in a MongoDb database. But if i add the whole json as a single document in a collection, whenever i do a query to look for a question : answer pair, the whole document is returned, since is the only one. If i try to make 2 documents("single", "multiple"), it will still return the whole "single" document. If i go even lower and do just "art", "biology", etc. documents i will have duplicates since i have "art" for both singleAnswer and multipleChoice. Should i just name the documents "single.art", "multiple.art". If so, what would a query for the below condition look like?

`if not "What is a sitar?" in dictionary["multiple"]["art"]:
   dictionary["multiple"]["art"]["What is a sitar?"] : 
                            ["Instrument","Food","Insect","Temple"] 

`

I have done all of this scenarios mentioned above except the last one, and i found that every time i query, it returns the whole object when all i need is a single question and it's answer(if it exists). Am i missing something or maybe i expect this to still work as a json(dictionary)? Thank you!

Edit:

Found this in MongoDb documentation. Would my scenario qualify as a hierarchical relationship? Meaning that every question belongs in a certain subject and every subject in it's own category (single, multiple)

Documents can be nested to express hierarchical relationships and to store structures such as arrays.

CodePudding user response:

I don't think your design is very smart. Dynamic field names are usually difficult to handle, the queries are complex and it is very hard to index them.

I would propose data model like this:

{ 
   type: "multiple",
   categroy: "art",
   questions: [
      {
         question: "What is a sitar?",
         choice: ["Instrument","Food","Insect","Temple","Village"],
         answers: ["Instrument","Village"]
      },
      {
         question: ...,
         choice: ...
      }
   ]
}

Or even one document for each question.

  • Related