0

所以,我的问题与MongoDB 关系相同:嵌入还是引用?但是我想知道是否应该将另一个嵌套级别(例如答案)嵌入或链接到架构中。

像这样:

Question
  body: "This is my question"
  Comments: [
    comment: {
      _id: ObjectId
      body: "This is a comment on the question"
    }
  ]
  Answers: [
    answer: {
      _id: ObjectId
      body: "This is an answer"
      Comments: [
        comment: {
          _id: ObjectId
          body: "This is a comment on this answer"
        }
      ]
    }
  ]

我的架构实际上与问题、答案或评论没有任何关系,但架构结构在嵌套和查询类型方面是相同的,例如在这种情况下会执行的编辑和排序。

此外,我意识到除了单层数组嵌套之外,不可能使用位置符号。所以answers.$.comments.$.body不可能...... https://jira.mongodb.org/browse/SERVER-831?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab

4

1 回答 1

1

While in an RDBMS system you more think about the data how is in a best structure, so it is about the Schema design not about the usage, usually with such technologies like mongoDB you more have to think about how the data will be used. Of course you have to consider consistency but more need to think about to store those data together what will be needed in the same time.

Anyway it is not a good idea to do a several levels of embedded lists why it will be a pain to manage, and has some limitations.

For example like here in stackoverflow, the question is viewed along with the answers, and also with comments. These are first level lists so no problem. You do not see just the first couple of comments so if you store them for example the first 4 along with the answer and all the others in a separated collection it will work while you click to view more.

Moreover, if we go from the view where the questions listed. You start with click on the question then you query the comments and the answers (so they can be separated collections) and if somebody likely to dig deeper can click on for an individual answer to show more related comments (so it can be again a separated collection.)

If you separate the collections handling will be much easier, collections will be probably not much bigger than putting them together. You will gain extreme much on that you only have to query (page in or keep in memory) data when it is really needed to view.

于 2013-09-12T07:49:22.510 回答