Mongodb Data Modeling

Source: Internet
Author: User
The previous section describes how to embed and reference A mongodb document. There are still many differences between MongoDB and relational database modeling, because MongoDB supports embedded objects and array types. Two approaches are available for MongoDB Modeling: Embedded (Embed) and Link ). So when will Embed be connected? What kind of relationship is between two entities?

The previous section describes how to embed and reference A mongodb document. There are still many differences between MongoDB and relational database modeling, because MongoDB supports embedded objects and array types. Two approaches are available for MongoDB Modeling: Embedded (Embed) and Link ). So when will Embed be connected? What kind of relationship is between two entities?

The previous section describes how to embed and reference A mongodb document. There are still many differences between MongoDB and relational database modeling, because MongoDB supports embedded objects and array types. Two approaches are available for MongoDB Modeling: Embedded (Embed) and Link ). So when will Embed be connected? It depends on the type of the relationship between two entities. One-to-one relationship: Embed. For example, the user information set has the Address field, and the Address field has the province, city, and county fields. The modeling is as follows: One-to-multiple relationship: A single article has multiple comments, which are 1-to-many relationships. Because MongoDB has a size limit of 16 MB for a single document (document) (higher than v1.8 ), this restriction should also be taken into consideration during design. Many-to-many relationship: There is a many-to-many relationship between students and courses. One student can select multiple courses, and multiple students participate in one course. Many-to-many connections use Linking, and the connection uses References to connect two sets. There are two types of MongoDB References: Manual reference and DBRefs. Manual References:The red box contains Manual References. to query the author information of an article, first find the article in The post Collection, and then find all user information in the user collection. However, in this scenario, users can comment on images, articles, and other resources. All comments are included in the comment set. If you only use Manual References, I cannot tell which type of resources the comment belongs to, pictures? Article ?. So we have DBRef. DBRefFormat: {$ ref: , $ Id: , $ Db: } $ Ref: Set Name; $ id: referenced id; $ db: Database Name; optional parameter. It can be seen that the structure of DBRef is more complex than that of Manual References, which occupies a large amount of space, but has powerful functions. To connect to a different database, DBRef is required for the example of the comment set mentioned above, mongoDB provides functions to parse DBRef. You do not need to manually write two queries like Manual References. Some suggestions are also provided on the MongoDB Data Modeling website. These suggestions provide some reference. The actual modeling needs to be analyzed based on specific requirements, and the Embed and Link operations (sorting, search, and modification) are often performed for data analysis.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.