One-to-one relationship: Embed. For example, the user information set has the Address field and the Address field has three fields: province, city, and county. The modeling is as follows:
One-to-many relationship: a single article has multiple comments, which is a one-to-many relationship.
Because MongoDB limits the size of a single document to 16 MB (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. A student can select multiple courses, and multiple students participate in one course.
Multiple-to-multiple connections use Linking), and the connection uses referencing References to connect two sets. There are two types of MongoDB References: one is to manually reference Manual References) and the other is DBRefs.
Manual References:
(User set)
(Post set)
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.
DBRef format:
{$ Ref: <value >,$ id: <value >,$ db: <value>}
$ 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, which operations are often sorted, searched, and modified by analyzing the data) to select Embed and Link.
Edit recommendations]
- MongoDB 2.0 official version released
- View the Compact Command one by one in the new functions of MongoDB 2.0
- Comprehensive Evaluation of mainstream NoSQL databases-MongoDB
- How to use MySQL to learn MongoDB
- MongoDB setup and simple operations in Windows