MongoDB Relation

Tram Ho

Mongodb relationships are NoSQL equivalents to links in relational databases. While they all perform similar goals, appear in similar cases, the nature of their behavior is completely different.

NoSQL relations vs RDBMS associations

The Relation (relational) data link from this model with other models. Deeper, this is exactly what links RDBMS is for. However, the way data is linked in NoSQL is completely different. In traditional relational databases, relationships are used to link one table to another. MongoDB and equivalent NoSQL databases are ” document-oriented ” (ie there are no tables but only records). So instead, links are drawn in between ” records .”

ActiveRecord

Mongoid (referencing)

Mongoid (embedding)

All of these classes are basically on implementing the link between one author have multiple post, 1 post posts have multiple comments. Looking at the code above we can see

embeds_many <=> has_many

embeds_in <=> belongs_to

But this has simplified it.

ActiveRecord

The relational database processes links through foreign keys referencing a single row of another table. For example, a post will have an author that references the ID of the record in the authors table to which it belongs. If an author has multiple posts , multiple post records will have the same author. author has no post_ids.

Relational database schema

Relational database schema

Mongoid References

One way the NoQuery database handles one to many relationships is through relationships. References are more similar to associations in a relational database rather than embedded (this section will be explained below). Relationships also use foreign keys, but they point from one document to another, instead of one record in one table to a record in another.

Mongoid Embeds

Another way the NoQuery database handles this type of relationship with many relationships is through embedding documents. A document is embedded in another document, essentially leading to a hash (hash) giant. For example, the author will embed multiple post documents and each post document will embed multiple comments . Each author is a hash containing an array of posts , each document containing an array of comments .

Embedding vs Referencing

Why does Mongo need multiple ways to define the same relationship with multiple relationships when ORMs like ActiveRecord only require one? Both embedding and reference are reasonable options, but each option serves more for specific use cases. There are a few things that will need to be considered before making a decision.

Will your data be connected from multiple points? If you need to access your data from multiple points, you should probably use Referencing . If your data is only relevant regarding its original document, Embedding is the way to go. Also important to consider are data consistency and document size. MongoDB documents may be limited to a maximum size of 4 MB, however, it is very unlikely that this will be the problem you will encounter soon.

Source : http://pat-whitrock.github.io/blog/2014/05/07/mongodb-relations/

Share the news now

Source : Viblo