Thanks to Julian Motz for kindly helping to peer review this article.
One of the biggest differences between SQL and NoSQL databases is JOIN. In relational databases, the SQL JOIN clause allows you to combine rows from two or more tables using a common field between them. For example, if you have tables of books
and publishers
, you can write SQL commands such as:
SELECT book.title, publisher.name
FROM book
LEFT JOIN book.publisher_id ON publisher.id;
In other words, the book
table has a publisher_id
field which references the id
field in the publisher
table.
This is practical, since a single publisher could offer thousands of books. If we ever need to update a publisher's details, we can change a single record. Data redundancy is minimized, since we don't need to repeat the publisher information for every book. The technique is known as normalization.
SQL databases offer a range of normalization and constraint features to ensure relationships are maintained.
NoSQL == No JOIN?
Not always …
Document-oriented databases such as MongoDB are designed to store denormalized data. Ideally, there should be no relationship between collections. If the same data is required in two or more documents, it must be repeated.
This can be frustrating, since there are few situations where you never need relational data. Fortunately, MongoDB 3.2 introduces a new $lookup
operator which can perform a LEFT-OUTER-JOIN-like operation on two or more collections. But there's a catch …
Continue reading %Using JOINs in MongoDB NoSQL Databases%
by Craig Buckler via SitePoint