简体   繁体   English

我应该如何构建我的 MongoDb 数据库? 大量的小文件或嵌入较少的文件?

[英]How should i structure my MongoDb database? A lot of small documents or embedded fewer documents?

I am new to python and very new to MongoDb.我是 python 的新手,对 MongoDb 非常陌生。 I made an application to store trivia questions, currently in a json.我做了一个存储琐事问题的应用程序,目前在 json 中。 This is how the overall structure looks like:这是整体结构的样子:

在此处输入图像描述

This is an example of single answer questions, in art category:这是艺术类别中单答案问题的示例: 在此处输入图像描述

And this is an example of multiple choice questions in art category:这是艺术类别中的多项选择题示例:

在此处输入图像描述

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:所以,要得到一个问题的答案,我会做: answers = dictionary["multiple"]["art"]["What is a sitar?"]我会得到:

["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.我想将我保存的所有问题和答案移动到 MongoDb 数据库中。 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.但是,如果我将整个 json 添加为集合中的单个文档,则每当我执行查询以查找问题:答案对时,都会返回整个文档,因为它是唯一的一个。 If i try to make 2 documents("single", "multiple"), it will still return the whole "single" document.如果我尝试制作 2 个文档(“单个”、“多个”),它仍然会返回整个“单个”文档。 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.如果我 go 甚至更低并且只做“艺术”、“生物学”等文件,我将有重复的文件,因为我对 singleAnswer 和 multipleChoice 都有“艺术”。 Should i just name the documents "single.art", "multiple.art".我是否应该将文件命名为“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).除了最后一个,我已经完成了上面提到的所有这些场景,我发现每次查询时,它都会返回整个 object,而我只需要一个问题并且它就是答案(如果存在)。 Am i missing something or maybe i expect this to still work as a json(dictionary)?我是否遗漏了什么,或者我希望它仍然可以作为 json(字典)工作? Thank you!谢谢!

Edit:编辑:

Found this in MongoDb documentation.在 MongoDb 文档中找到了这个。 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.可以嵌套文档以表达层次关系并存储 arrays 等结构。

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:我会提出这样的数据 model :

{ 
   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.甚至每个问题都有一份文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM