MongoDB relationships: embed or reference?

后端 未结 11 2408
夕颜
夕颜 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:57

    MongoDB gives freedom to be schema-less and this feature can result in pain in the long term if not thought or planned well,

    There are 2 options either Embed or Reference. I will not go through definitions as the above answers have well defined them.

    When embedding you should answer one question is your embedded document going to grow, if yes then how much (remember there is a limit of 16 MB per document) So if you have something like a comment on a post, what is the limit of comment count, if that post goes viral and people start adding comments. In such cases, reference could be a better option (but even reference can grow and reach 16 MB limit).

    So how to balance it, the answer is a combination of different patterns, check these links, and create your own mix and match based on your use case.

    https://www.mongodb.com/blog/post/building-with-patterns-a-summary

    https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1

    0 讨论(0)
  • 2020-11-21 06:01

    If I want to edit a specified comment, how do I get its content and its question?

    If you had kept track of the number of comments and the index of the comment you wanted to alter, you could use the dot operator (SO example).

    You could do f.ex.

    db.questions.update(
        {
            "title": "aaa"       
        }, 
        { 
            "comments.0.contents": "new text"
        }
    )
    

    (as another way to edit the comments inside the question)

    0 讨论(0)
  • 2020-11-21 06:02

    I know this is quite old but if you are looking for the answer to the OP's question on how to return only specified comment, you can use the $ (query) operator like this:

    db.question.update({'comments.content': 'xxx'}, {'comments.$': true})
    
    0 讨论(0)
  • 2020-11-21 06:04

    This is more an art than a science. The Mongo Documentation on Schemas is a good reference, but here are some things to consider:

    • Put as much in as possible

      The joy of a Document database is that it eliminates lots of Joins. Your first instinct should be to place as much in a single document as you can. Because MongoDB documents have structure, and because you can efficiently query within that structure (this means that you can take the part of the document that you need, so document size shouldn't worry you much) there is no immediate need to normalize data like you would in SQL. In particular any data that is not useful apart from its parent document should be part of the same document.

    • Separate data that can be referred to from multiple places into its own collection.

      This is not so much a "storage space" issue as it is a "data consistency" issue. If many records will refer to the same data it is more efficient and less error prone to update a single record and keep references to it in other places.

    • Document size considerations

      MongoDB imposes a 4MB (16MB with 1.8) size limit on a single document. In a world of GB of data this sounds small, but it is also 30 thousand tweets or 250 typical Stack Overflow answers or 20 flicker photos. On the other hand, this is far more information than one might want to present at one time on a typical web page. First consider what will make your queries easier. In many cases concern about document sizes will be premature optimization.

    • Complex data structures:

      MongoDB can store arbitrary deep nested data structures, but cannot search them efficiently. If your data forms a tree, forest or graph, you effectively need to store each node and its edges in a separate document. (Note that there are data stores specifically designed for this type of data that one should consider as well)

      It has also been pointed out than it is impossible to return a subset of elements in a document. If you need to pick-and-choose a few bits of each document, it will be easier to separate them out.

    • Data Consistency

      MongoDB makes a trade off between efficiency and consistency. The rule is changes to a single document are always atomic, while updates to multiple documents should never be assumed to be atomic. There is also no way to "lock" a record on the server (you can build this into the client's logic using for example a "lock" field). When you design your schema consider how you will keep your data consistent. Generally, the more that you keep in a document the better.

    For what you are describing, I would embed the comments, and give each comment an id field with an ObjectID. The ObjectID has a time stamp embedded in it so you can use that instead of created at if you like.

    0 讨论(0)
  • 2020-11-21 06:08

    In general, embed is good if you have one-to-one or one-to-many relationships between entities, and reference is good if you have many-to-many relationships.

    0 讨论(0)
提交回复
热议问题