Database Reference
In-Depth Information
Creating a Relationship
Creating a relationship between two nodes with Neo4jClient can be handled in a few different ways, but the most
efficient is to retrieve the nodes and create the relationship in the same cypher statement. As shown in Listing 7-4, the
query sets up a relationship between two users by using the FOLLOWS relationship type.
Both the start and end nodes used to create a relationship must already be saved within the database before
the relationship can be saved.
Note
Listing 7-4. Finding Two Nodes and Creating a Relationship between Them
var _graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"));
_graphClient.Cypher
.Match("(user1:User)", "(user2:User)")
.Where<User>(user1 => user1.userId == "10")
.AndWhere<User>(user2 => user2.userId == "1")
.Create("user1-[:FOLLOWS]->user2")
.ExecuteWithoutResults();
Retrieving Relationships
Once a relationship has been created between two or more nodes, then the relationship can be retrieved based on
one of the nodes within the relationship (Listing 7-5).
Listing 7-5. Retrieving Relationships
var _graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"));
_graphClient.Cypher
. Match("(user1:User)-[f:FOLLOWS]-(user2:User)")
.Where<User>(user1 => user1.userId == "10")
.AndWhere<User>(user2 => user2.userId == "1")
.Return((f) => new {
Relationship = f.As< Relationship >()
})
.Results.Single();
Deleting a Relationship
Once a relationship's graph id has been set and saved into the database, it becomes eligible to be removed when
necessary. In order to remove a relationship, it must be set as a relationship object instance and then the delete
method for the relationship (Listing 7-6) can be called.
 
 
Search WWH ::




Custom Search