Reputation: 68
I am doing some research into Graph Database Systems as part of POC to possibly move a system across to. I am really new to the concept and come from a RDBMS background.
I have a model (schema?) that looks like:
Person-[HAS_NAME {type:First|Middle|Last}]-Name
Person-[WAS_BORN_ON]-DateOfBirth
Person-[RESIDES_AT {type:Current|Previous}]-Address
I am able to store this data perfectly using Neo4J and Neo4JClient in a C# application.
Where I am falling flat on my backside is that I want to get out of the store, a list of People and all nodes that are connected to the person (eg their Names, DateOfBirth and Addresses) where certain conditions are met,
I have started with this:
MATCH (dob:DateOfBirth)-[WAS_BORN_ON]-(person:Person)-[HAS_NAME]-(name:Name) WHERE dob.Id = '1954-05-09' RETURN person, dob, name
And it produces something like this which is great:
But I want to restrict it to people that have a (last) name of "williams" so I go with this
MATCH (dob:DateOfBirth)-[WAS_BORN_ON]-(person:Person)-[HAS_NAME]-(name:Name) WHERE dob.Id = '1954-05-09' and name.Value = 'Williams' RETURN person, dob, name"
Unfortunately it removes all the other names:
Unfortunately I want this:
Upvotes: 0
Views: 81
Reputation: 48
I would have gone the easy route...
match (dob:DateOfBirth{Id: "1954-05-09"})--(p:Person)--(surname:Name{Value: "Williams"})
optional match (p)--(n:Name)
return dob, p, n
alternatively with a WHERE clause:
match (dob:DateOfBirth)--(p:Person)--(surname:Name)
optional match (p)--(n:Name)
where dob.Id = "1954-05-09" and surname.Value = "Williams"
return dob, p, n
But then again, I'm not the expert on performance; I focus first on simplicity.
Upvotes: 0
Reputation: 2445
Maybe something like this would work?
MATCH (dob:DateOfBirth{Id: "1954-05-09"})<-[:WAS_BORN_ON]-(person:Person)
WITH dob, person
MATCH (person)-[:HAS_NAME]->(surname:Name{Value: "Williams"})
WITH person, dob
MATCH (person)-[:HAS_NAME]->(name:Name)
RETURN person, dob, name
Edit: Updated query to improve performance as suggested by ulkas.
Upvotes: 1