In this tutorial you'll write queries against a small movie graph, and every one of them runs in your browser tab. You'll find a node, follow relationships, count and sort results, and create a node of your own.
Before you start
Nothing to set up. The queries run on Relate, an open-source embedded graph database that compiles to WebAssembly, so there is no server behind this page. The graph is built by the first block below, and your browser runs it as soon as this page loads. Every query after it has a Run button, so press Run on each one in order and compare what you see with the table underneath. You can edit any of these queries before you run it, and Reset puts the original back.
The starting graph
This is the statement that builds the graph. It uses MERGE, which creates a node or relationship only if it doesn't already exist, so running it twice wouldn't duplicate anything.
MERGE (tom:Person {name: 'Tom Hanks'}) SET tom.born = date('1956-07-09')
MERGE (tim:Person {name: 'Tim Allen'})
MERGE (don:Person {name: 'Don Rickles'})
MERGE (jim:Person {name: 'Jim Varney'})
MERGE (meg:Person {name: 'Meg Ryan'})
MERGE (toy:Movie {title: 'Toy Story'}) SET toy.released = 1995
MERGE (splash:Movie {title: 'Splash'}) SET splash.released = 1984
MERGE (big:Movie {title: 'Big'}) SET big.released = 1988
MERGE (sleepless:Movie {title: 'Sleepless in Seattle'}) SET sleepless.released = 1993
MERGE (tom)-[:ACTED_IN]->(toy)
MERGE (tim)-[:ACTED_IN]->(toy)
MERGE (don)-[:ACTED_IN]->(toy)
MERGE (jim)-[:ACTED_IN]->(toy)
MERGE (tom)-[:ACTED_IN]->(splash)
MERGE (tom)-[:ACTED_IN]->(big)
MERGE (tom)-[:ACTED_IN]->(sleepless)
MERGE (meg)-[:ACTED_IN]->(sleepless)
MERGE (meg)-[:ACTED_IN]->(splash);
MATCH (toy:Movie {title: 'Toy Story'})
UNWIND ['Adventure', 'Animation', 'Children', 'Comedy', 'Fantasy'] AS name
MERGE (g:Genre {name: name})
MERGE (toy)-[:IN_GENRE]->(g);The following query runs automatically and counts the nodes under each label:
MATCH (n)
RETURN labels(n)[0] AS label, count(n) AS total
ORDER BY labelYou should see three rows:
| label | total |
|---|---|
| Genre | 5 |
| Movie | 4 |
| Person | 5 |
1. Find a person
A Person node has a name property. Find the one named Tom Hanks and return two of his properties:
You should see one row:
| name | born |
|---|---|
| Tom Hanks | 1956-07-09 |
MATCH describes what to look for, WHERE narrows it, and RETURN chooses what comes back.
2. Follow a relationship
People connect to movies through ACTED_IN. The arrow points from the person to the movie, so to start from the movie you point it the other way. List the actors in Toy Story:
You should see four rows:
| actor |
|---|
| Don Rickles |
| Jim Varney |
| Tim Allen |
| Tom Hanks |
The middle of the MATCH is a pattern, and it reads almost like a sentence, a movie acted in by a person.
3. Follow a different relationship
Movies belong to genres through IN_GENRE. Swap the relationship type and the label, and you have a new question:
You should see five rows:
| genre |
|---|
| Adventure |
| Animation |
| Children |
| Comedy |
| Fantasy |
4. Count and sort
Start from the person this time and count the films Tom Hanks acted in:
You should see one row:
| films |
|---|
| 4 |
Now list them, earliest first, and keep the first three:
You should see three rows:
| title | released |
|---|---|
| Splash | 1984 |
| Big | 1988 |
| Sleepless in Seattle | 1993 |
5. Follow two relationships
Meg Ryan is in the graph too. To find who she acted alongside, match a second person who acted in the same movie, all in a single pattern:
You should see two rows:
| title | costar |
|---|---|
| Sleepless in Seattle | Tom Hanks |
| Splash | Tom Hanks |
6. Create your own node
So far you've only read data. Replace Your Name with your own name and add a person to the graph:
You should see one row containing the name you typed. Change the name in the next query to match, and find the node again:
You should see the same row. The node lasts until you reload this page.
Run it yourself with Relate
This page is a demo of Relate, and you can run every query in it on your own machine. Install the Relate command line tool, open the interactive shell on an in-memory database, and paste in the seed statements and queries from above:
curl -sSfL https://relatedb.ai/install.sh | bash
relate queryStatements in the shell end with a semicolon, so add one to the end of each query.
Relate is an open-source embedded graph database that speaks Cypher. It runs inside your own process with no server to deploy and no signup, and it's built to be driven by AI agents as well as people. It has an agent skill that teaches an assistant to create a database, load your data and answer questions by writing Cypher. Use it when you want to run Cypher locally without provisioning anything, for an agent's memory, for test fixtures, or for analyzing a dataset in a script. It holds the graph in memory with a single writer, so it isn't the right choice for a graph larger than RAM or for many simultaneous writers.
Next steps
The Neo4j Fundamentals course takes these patterns further, against a larger movie graph, and runs the same way in your browser. At the end of it you'll create a real cloud database and import the data model you built.
Comments (0)
Loading comments...