Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-24-2021 01:51 AM
I am a complete beginner so forgive this simple question.
I have database which contains nodes for three people, a man, his wife and his sister.
I don't really want to have two separate relationships for this if at all possible.
The code I have used is as follows
MATCH
(a:Person),
(b:Person)
WHERE a.name = 'Smith, Joseph' AND b.name = 'Jones, Dorothy'
CREATE (a)-[r:Marital {name: a.name + '<->' + b.name}]->(b)
MATCH
(c:Person),
(d:Person)
WHERE c.name = 'Smith, Joseph' AND d.name = 'Smith, Sarah'
CREATE (c)-[r:Filial {name: c.name + '<->' + d.name}]->(d)
Thanks so much!
12-24-2021 02:34 AM
Welcome Botanic Macaroni!
In neo4j, relationships are created with, and always have one and only one direction. It is possible to create two or more relationships between two nodes, and the same type of relationship can have opposite directions. To have two relationships of similar typ between two nodes is often unnecessary and is then often not good practise.
It is a often good practice to use verbs for naming relationships.
There are many ways to create and name these two kind of relations between people.
One alternative could be to use a "IS_A_SIBLING_OF" relationship between brothers and sisters, and it might also be possible to use a "IS_MARRIED_TO" between husband and wife.
When you try to MATCH these kinds of relationships, then you don't need to specify the direction.
You know that if A is a sibling of B, then B will also be a sibling of A. And if B is married to C, then C will also be married to B. So in this case the directions of the relationships does not matter.
To sum up:
In neo4j, you always need to specify a direction when you create relationship. In this case the created direction could be arbitrary. It doesn't matter which direction you choose.
In neo4j, you do not need to specify a direction when you match relationship. In that case you will match the existence of a relationship type regardless of the direction of the relationship type.
Good Yule!
12-24-2021 02:46 AM
Thank you so much for this, hugely helpful to know that I am not looking for something that doesn't happen!
I don's suppose that you know whether I can add attributes to the relationship, like the marriage dates?
Thanks again, Happy Christmas!
12-24-2021 04:08 AM
Yes, a relationship can have properties in key-value pairs, like: {married: YYYY-MM-DD}.
12-24-2021 04:28 AM
Thank you again, I think I can make some good progress now.
All the sessions of the conference are now available online