MongoDB relationships: embed or reference?

后端 未结 11 2381
夕颜
夕颜 2020-11-21 05:34

I\'m new to MongoDB--coming from a relational database background. I want to design a question structure with some comments, but I don\'t know which relationship to use for

11条回答
  •  臣服心动
    2020-11-21 05:45

    Well, I'm a bit late but still would like to share my way of schema creation.

    I have schemas for everything that can be described by a word, like you would do it in the classical OOP.

    E.G.

    • Comment
    • Account
    • User
    • Blogpost
    • ...

    Every schema can be saved as a Document or Subdocument, so I declare this for each schema.

    Document:

    • Can be used as a reference. (E.g. the user made a comment -> comment has a "made by" reference to user)
    • Is a "Root" in you application. (E.g. the blogpost -> there is a page about the blogpost)

    Subdocument:

    • Can only be used once / is never a reference. (E.g. Comment is saved in the blogpost)
    • Is never a "Root" in you application. (The comment just shows up in the blogpost page but the page is still about the blogpost)

提交回复
热议问题