简体   繁体   中英

MongoDB is non-relational, but Mongo Gem is relational

So just have a quick question. I would like to clarify this to myself:

MongoDB according to the documentation is a non-relational database. However gem for mongo can make models relational.

So the question is it gem implementation that it allows to query database in a standard AR way?

Any explanations are welcomed.

Let's make one point absolutely clear. MongoDB does not internally do joins in any way whatsoever. It cannot be done as it is against the principles of the general design. At least for now anyway. So when we are talking about "relational" dataabases, those are doing internal joins on the server.

What your particular library (whatever that is) or otherwise referred to as an ODM ( Object Document Mapper) is doing is something that simulates a join on the "client" ( your running code ) side, but this is not an actual join.

So this allows you do do things such as:

  1. Fetch an item and then all it's related items that exist in other collections.

  2. Make what is actually an "embedded" document (in MongoDB terms ) appear just as another external relation as it would with a relational datastore.

So in the case of 1 what will actually happen in your client code is that as a "master" document is retrived there will be an additional request (or requests) made to the database in order to fetch the related documents. This is typically done as one or the other document collections having the related key information (either all children or the parent) in order that the results can be referenced.

The apparent "joining" then actually happens on the client side and not on the server.

The limitation here is that your are typically restricted to not being able to "filter" parent document results based on the content within the children. This may vary on the implementation, but any parent that stores references to all of it's children with have that restriction, for example that is.

On the other hand as is the case with 2 , as the documents are actually "embedded" within each other, there is only one request made to the database in order to retrieve a document and all of it's "related" entries. This is in fact one of the major design points and advantages of using MongoDB for storage where this case suits your needs.

As in this case the data is already "pre-joined" there is no need to perform any "join" as such and all reads and writes can be done in an atomic way.


Anyhow that should serve as a primer to the basic differences. Explaining every aspect of why "non-relational" and other NoSQL stores exist over the alternate relational SQL stores is far to broad a topic to answer here. There is plenty of reading available on the web.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM