Data modeling in MongoDB

Source: Internet
Author: User

One to one relationship: Embed, such as the user information collection has an address field, the Address field has three fields of province, city and county. The modeling is as follows:

650) this.width=650; "class=" Fit-image "title=" QQ20120725190049 "alt=" QQ20120725190049 "src="/HTTP/ Images.51cto.com/files/uploadimg/20120726/0833430.jpg "border=" 0 "/>

650) this.width=650; "class=" Fit-image "title=" QQ20120725185306 "alt=" QQ20120725185306 "src="/HTTP/ Images.51cto.com/files/uploadimg/20120726/0833431.jpg "border=" 0 "width=" 498 "/>

One-to-many relationship: an article with multiple comments for 1-to-many relationships

650) this.width=650; "class=" Fit-image "title=" QQ20120725185727 "alt=" QQ20120725185727 "src="/HTTP/ Images.51cto.com/files/uploadimg/20120726/0833432.jpg "border=" 0 "/>650) this.width=650;" Class= "Fit-image" Title= "QQ20120725190617" alt= "QQ20120725190617" src= "http://images.51cto.com/files/uploadimg/20120726/0833433". JPG "border=" 0 "width=" 498 "/>

Since MongoDB has a size limit of 16M (higher than v1.8) for a single document, this restriction is also taken into account at design time.

Many-to-many relationships: students and courses are many-to-many relationships, one student can choose multiple courses, and one course has multiple students involved.

650) this.width=650; "class=" Fit-image "title=" QQ20120725190110 "alt=" QQ20120725190110 "src="/HTTP/ Images.51cto.com/files/uploadimg/20120726/0833434.jpg "border=" 0 "/>

Many-to-many uses a connection (linking), which connects two collections by reference (References). There are two kinds of MongoDB References: one is manual reference (Manual References) and the other is dbrefs.

Manual References:

650) this.width=650; "class=" Fit-image "title=" QQ20120725192451 "alt=" QQ20120725192451 "src="/HTTP/ Images.51cto.com/files/uploadimg/20120726/0833435.jpg "border=" 0 "width=" 498 "/> (User collection)

650) this.width=650; "class=" Fit-image "title=" QQ20120725192634 "alt=" QQ20120725192634 "src="/HTTP/ Images.51cto.com/files/uploadimg/20120726/0833436.jpg "border=" 0 "width=" 498 "/> (Post Collection)

Red Box Place is manual References, if you want to query the author of an article information, first in the Post collection to find out the article, and then in the user collection to find out all the users information. But if there is such a scene: Users can comment on pictures, articles and other resources, all the comments are placed in the comment collection, if you just use manual References, it is not clear what kind of resources the comment is, picture? Article?. So with the dbref.

form of Dbref:

{$ref: <value>, $id: <value>, $db: <value>}

$ref: Collection name; $id: Reference ID; $db: database name, optional parameter.

Can see the structure of dbref than manual references complex, occupy a large space, but the function is also powerful, if you want to cross-database connection, the above comment collection example, you need to use DBREF,MONGODB provides a function to parse dbref, Not like manual references need to manually write two queries.

650) this.width=650; "class=" Fit-image "title=" QQ20120725195916 "alt=" QQ20120725195916 "src="/HTTP/ Images.51cto.com/files/uploadimg/20120726/0833437.jpg "border=" 0 "width=" 498 "/>

Data modeling for MongoDB MongoDB website also gives some suggestions. These recommendations provide some reference, and the actual modeling needs to be analyzed according to the specific requirements, analyzing what operations (sorting, finding, modifying) are often performed by the data to select Embed and link.


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1542484

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.