Here’s a short practice exercise to help you get some hands-on experience with Neo4j:
- Create a new database in the Neo4j browser and give it a name.
- Create three nodes representing people in the database. Label the nodes as ‘Person’ and give each node a ‘name’ property.
- Create two relationships between the nodes. One relationship should be labeled ‘FRIEND’ and the other should be labeled ‘FOLLOWS’.
- Use Cypher to write a query that returns all nodes in the database that are connected to the first node by either the ‘FRIEND’ or ‘FOLLOWS’ relationship.
- Use the visualization tool to explore the graph and verify that your query returned the expected results.
Here’s an example of what your Cypher query might look like:
cssCopy codeMATCH (p1:Person {name: 'Alice'})-[:FRIEND|FOLLOWS]->(p2:Person)
RETURN p2
This query finds the node labeled ‘Person’ with the name ‘Alice’ and returns all nodes that are connected to it by either the ‘FRIEND’ or ‘FOLLOWS’ relationship.
To use the visualization tool, switch to the ‘Graph’ tab in the Neo4j browser and zoom in and out, pan around, and explore the graph to see the nodes and relationships that were returned by your query.
Once you’ve completed this exercise, you can try modifying the database and the Cypher query to perform other operations, such as adding additional nodes and relationships, querying for specific patterns, or performing other types of graph analysis.