How MongoDB handles many-to-many relationships

Source: Internet
Author: User
Problem Description: For example, there is a team table in the relational database, a user table, the two are many-to-many relationships, that is, a team can have multiple user, a user may belong to more than one team, how does such a relationship in MongoDB storage?
If it is a pair of more, I know can be nested, but do not know how to deal with many-to-many relationships, please advise. "From the mountain Thief" question answer: How to deal with a lot of relationships is the essence of NoSQL. In theory, storage can be done in a single collection, but in practice this is a rare situation. This is due to the diversity of queries, if there is only one type of query, then this many-to-many relationship in a well-designed set, although there will be a lot of redundancy, but the efficiency must be the highest. The key to how to design such a database is to see how many queries you have, how often each one is, and what other requirements are used. For different queries, the performance of the same database design is a big difference. There is also a point, generally do not split into three sets, this is the traditional relational database thinking mode. It's a nosql thing to think about in a different way. The common scenario is to split into two collections and then have a portion of redundancy to index the most commonly used queries. This has to be practiced. "Mars" 1, Team inside a teammates: [] Save user's _id or other index value, so that you know what a Team has user
2, the user can check the team's find ({teammates: _id}), so that a user in which team "ideal" and the relational database similar, separate two document storage,
Then one of the document's ID lists exists in another document "Jarod"

How MongoDB handles many-to-many relationships

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.