MongoDB Data Modeling

Source: Internet
Author: User
Tags mongodb setup

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]

  1. MongoDB 2.0 official version released
  2. View the Compact Command one by one in the new functions of MongoDB 2.0
  3. Comprehensive Evaluation of mainstream NoSQL databases-MongoDB
  4. How to use MySQL to learn MongoDB
  5. MongoDB setup and simple operations in Windows

Related Article

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.