DEV Community

Discussion on: How do you handle many-to-many relationships in Mongo?

Collapse
 
dmfay profile image
Dian Fay

That's a quintessential relational data model, and document databases really aren't designed for that use case. It can be done, as Ankur demonstrates, but awkwardly and without the safeguards of foreign key constraints.

Document dbs shine when you have sets of hierarchies with few to no interrelationships. In other cases, it's like using a wrench to drive nails.

Collapse
 
_suvesh_ profile image
Suvesh Misra

Hi, I see Tinder uses MongoDB and they use a M-M relationship with the swiping feature. How do you think that would work? Also, what DB do you recommend for M-M?

Collapse
 
biztux profile image
George Embrey • Edited

The ONLY truly viable way to ensure the integrity of data in Many to Many relationships is to use database engines which are designed to correctly handle foreign key constraints, RDBMs are the typical way to do this, many Modern RDBMs also handle JSON data extremely well and can store Documents in JSON columns whilst still maintaining full referential integrity. (Yes there are some who would suggest doing it in code, most who do so have never had to fixup the data messes this can and often does cause).

PostgreSQL (Amazon Redshift), MySQL 8 (Amazon RDS / Aurora MySQL 8), SQL Server (Amazon RDS SQL Server / Azure SQL Database), these three are very widely used and each have JSON functions which enable many of the functions of NOSQL databases to be implemented within an RDBMs.