Moody
Moody

Reputation: 833

Graph database schema design - Is this suitable for neo4j?

Scenario: a simple address book where a user can create his own contacts and organize them by adding them in groups. A contact may have multiple addresses.

I have created the following diagram: ![schema-design][1]

I want to query all the contacts who are placed in group x and live in country y.

Is this schema design good enough for those purposes (I want to use the neo4j database)?

Upvotes: 3

Views: 320

Answers (2)

gaurav.singharoy
gaurav.singharoy

Reputation: 3921

One option is to have another node from address for country, as pointed out by stefan Armbruster. If you do not want to change the data structure, just add an index to the field "country" of Address. Then you can have a query

MATCH (:Group{name:'family'})<-[:placed_in_group]-(contact)-[:lives-at]->(:Address{country:'US'})

Upvotes: 2

Stefan Armbruster
Stefan Armbruster

Reputation: 39915

It looks like the notion of country should be a first class citizen in your graph since your query depends on it. Graph model design typically gets influenced a lot by your query patterns.

So I suggest to have a node labeled Country for each country and connect the Address node with :LOCATED_IN relationships to the country. (consequently drop the country property from the address nodes).

With that change your query is as easy as:

MATCH (:Group{name:'family'})<-[:placed_in_group]-(contact)-[:lives-at]->()-[:LOCATED_IN]->(:Country{name:'US'})
RETURN contact

Upvotes: 3

Related Questions