Nosql many-to-many

What is an acceptable template for handling many-to-many relationships in document database design?

+7
source share
2 answers

How you want to model many-to-many will depend on what queries you want to ask, how you want to update data, etc. .... Let's say that we have bindings to bars in many cases of the mod.

You can simulate foo as

{ 'bars': ['bar1', 'bar2', 'bar3'] } 

and model the panel as

 { 'foos': ['foo_x', 'foo_y', 'foo_z'] } 

Or you could model the graph or the relationship between foo and bar as separate documents themselves

 { from: 'foo1', to: 'bar1' } { from: 'foo1', to: 'bar2' } { from: 'foo2', to: 'bar3 } { from 'foo3', to: 'bar3' } 

There are many other ways. How you want to do this will depend on the questions you want to ask, the operations you want to support, what you want to be effective, and the indexing available in the database.

+3
source

Assuming that we are talking about cases where relationships are really necessary, and not those that exist only because SQL handles relationships better than complex objects, the design is similar to the standard for SQL - from two to several relationships.

The key difference is that you have multi-valued fields, so instead of a third document / table that records single joins as a pair of identifiers, you have a list of identifiers in each document.

If you are faced with situations where this list is too long, you probably look at the fact that it is better to handle search indexing than relationships.

+2
source

All Articles