I have some relations between persons in my graph.
my data (generate script below)
create (s:Person {name: "SUE"})
create(d:Person {name: "DAVID"})
create(j:Person {name: "JACK"})
create(m:Person {name: "MARY"})
create(js:Person {name: "JASON"})
create(b:Person {name: "BOB"})
create(a1:Adress {id:1})
create(a2:Adress {id:2})
create(a3:Adress {id:3})
create(a4:Adress {id:4})
create(a5:Adress {id:5})
merge (d)-[:MOTHER]->(s)
merge(j)-[:MOTHER]->(s)
merge(js)-[:MOTHER]->(m)
merge(b)-[:MOTHER]->(m)
merge(b)-[:CURRENT_ADRESS]->(a1)
merge(js)-[:CURRENT_ADRESS]->(a2)
merge(j)-[:CURRENT_ADRESS]->(a3)
merge(s)-[:CURRENT_ADRESS]->(a4)
merge(d)-[:CURRENT_ADRESS]->(a5)
;
I can get mothers who live with her child:
MATCH (p:Person)-[:CURRENT_ADRESS]->(a:Adress)<-[:CURRENT_ADRESS]-(t), (t)-[:MOTHER]->(p)
return p.name,t.name
p.name t.name
MARY JASON
but i want to get mothers who is not living with any child of her.
How can i do that in Cyper?
exists() The function exists() returns true if a match for the given pattern exists in the graph, or if the specified property exists in the node, relationship or map. null is returned if the input argument is null .
In Cypher, null is used to represent missing or undefined values. Conceptually, null means a missing unknown value and it is treated somewhat differently from other values. For example getting a property from a node that does not have said property produces null .
The MATCH clause allows you to specify the patterns Neo4j will search for in the database.
Actually in your graph, everybody is living at a different address due to different identifiers.
Let's build a graph example introducing the sister which lives at the same address :
CREATE
(p:Person)-[:MOTHER]->(m:Person),
(p)-[:FATHER]->(f:Person),
(p)-[:SISTER]->(s:Person),
(p)-[:CURRENT_ADDRESS]->(a:Adress),
(m)-[:CURRENT_ADDRESS]->(b:Adress),
(f)-[:CURRENT_ADDRESS]->(c:Adress),
(s)-[:CURRENT_ADDRESS]->(a)
Now this is very simple, match family members that don't have a CURRENT_ADDRESS
relationship in depth2 to the family member :
MATCH (p:Person)-[:MOTHER|:FATHER|:SISTER]->(familyMember)
WHERE NOT EXISTS((p)-[:CURRENT_ADDRESS*2]-(familyMember))
RETURN familyMember
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With