Relationships
In order to tell how nodes are associated with each another, we need to create relationships / edges between the nodes.
Creating Relationships
In order to tell that user with the ID 4 lives in country with the ID of 2, we could do this:
MATCH (p: Person { userId: 4 }) WITH p MATCH (c: Country { countryId: 1 }) CREATE (p)-[r:LIVES_IN]->(c)
Now, in order to also associate user with email britpop to country with a name of Sweden, we could do this:
MATCH (p: Person { email: "britpop@gmail.com" }) WITH p MATCH (c: Country { name: "Sweden" }) CREATE (p)-[r:LIVES_IN]->(c)
And we could go on further by declaring that user with the ID of 4 has the hobby with the name of icehockey:
MATCH (p: Person { userId: 4 }) WITH p MATCH (h: Hobby { name: "Icehockey" }) CREATE (p)-[r:HAS_HOBBY_OF]->(h)
Finding Node(s) with Relationships
To find the user with the name Tom and all it's relationships, we can do:
Example Output
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| n | r | b |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| (:Person {firstname: "Tom", city: "Pori", userId: 4, age: 18, email: "tom@gmail.com", lastname: "Jones"}) | [:LIVES_IN] | (:Country {name: "Finland", countryId: 1}) |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row
And of course this could be used with different filters, so to for example find all persons with relationships, this could be used:
Example Output
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| n | r | b |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| (:Person {firstname: "Tom", city: "Pori", userId: 4, age: 18, email: "tom@gmail.com", lastname: "Jones"}) | [:LIVES_IN] | (:Country {name: "Finland", countryId: 1}) |
| (:Person {firstname: "Tom", city: "Pori", userId: 4, age: 18, email: "tom@gmail.com", lastname: "Jones"}) | [:LIVES_IN] | (:Country {name: "Finland", countryId: 1}) |
| (:Person {firstname: "Brittany", city: "Nokia", userId: 5, email: "britpop@gmail.com", age: 28, lastname: "Pop"}) | [:LIVES_IN] | (:Country {name: "Sweden", countryId: 2}) |
| (:Person {firstname: "Brittany", city: "Nokia", userId: 5, email: "britpop@gmail.com", age: 28, lastname: "Pop"}) | [:LIVES_IN] | (:Country {name: "Sweden", countryId: 2}) |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
4 rows
Relationship Properties
Relationships can also contain properties. For example, to add a property for the LIVES_IN relationship between the person Tom and country Finland, this could be done: