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.