How to handle many to many relationships in Graph database?

Viewed 51

How to model Many-to-Many relationship in Graph databases (Property graph database)

In the below picture, Company/creditor files their claim to court when the borrower goes bankrupt and there will be an authorised representative for the each company/creditor to the case. How to model this scenario ?

Find who is representative of given creditor? Trick is creditor can have multiple claim on multiple case and multiple authorized representative.

Any suggestion ? If possible in Apache Tinkerpop Gremlin way of handling.

Many to Many Relationship

1 Answers

Generally speaking, one option is to connect the Representative directly to the Case. This will eliminate the need to add a dynamic label (Although we don't know what you want to query yet).

On neo4j, it can be modeled like this:

MERGE (a:Representative {key: 1})
MERGE (b:Representative {key: 2})
MERGE (c:Representative {key: 3})
MERGE (d:Company{key: 1})
MERGE (e:Company{key: 2})
MERGE (f:Case{key: 1})
MERGE (g:Case{key: 2})

MERGE (a)-[:CASE]-(f)
MERGE (b)-[:CASE]-(g)
MERGE (c)-[:CASE]-(g)
MERGE (d)-[:CREDITOR]-(f)
MERGE (d)-[:CREDITOR]-(g)
MERGE (e)-[:CREDITOR]-(g)
MERGE (a)-[:CONTACT]-(d)
MERGE (b)-[:CONTACT]-(d)
MERGE (c)-[:CONTACT]-(e)

And look like this: graph

Related