Tutorial: Create a graph data model

This tutorial is designed to help you understand how to model your data based on what you intend to use it for. You will use the Movies example dataset as the main resource.

For an interactive course on the fundamentals of data modeling, see GraphAcademy.

Define the domain

In this tutorial, you will use the Movies example dataset, the domain includes movies, people who acted or directed movies, and users who rated movies. It is in the connections (relationships) between these entities that you find insights about your domain.

Define the use case

With the domain defined, you need to identify your application use cases. In other words, what questions are you trying to answer?

You can make a list of questions to help you identify the application use cases. The questions will help you define what you need from the application, and what data must be included in the graph.

For this tutorial, your application should be able to answer these questions:

  • Which people acted in a movie?

  • Which person directed a movie?

  • Which movies did a person act in?

  • How many users rated a movie?

  • Who was the youngest person to act in a movie?

  • Which role did a person play in a movie?

  • Which is the highest rated movie in a particular year according to imDB?

  • Which drama movies did an actor act in?

  • Which users gave a movie a rating of 5?

Define the purpose

When designing a graph data model for an application, you may need both a data model and an instance model.

Data model

The data model describes the nodes and relationships in the domain and includes labels, types, and properties. It doesn’t contain any data but shows what information might be needed to answer the use cases.

At this stage, you can opt to use no-code tools to visualize your plan. With Arrows.app, for example, you can draft a data model that includes node labels, relationship types, and properties:

With your example domain and initial questions in mind, you can list the information you need to have in your first model:

  • Differentiation between a person who acted in a movie, who directed a movie, and who rated a movie.

  • What ratings were given, how many there are, and when they were submitted.

  • Which role an actor played in a movie and what their age is.

  • The genres of the movies.

  • Etc.

Note that in the model, labels, relationship types, and property keys follow a certain syntax. In Cypher®, these are called identifiers and they are case-sensitive, as are string values.

See the Cypher style guide for more information. While not mandatory, it is recommended that:

  • Labels are capitalized and should be CamelCase (e.g., Person, Movie, ImdbUser).

  • Relationship types are written with all capital letters and an underscore character as a separator (e.g., DIRECTED, ACTED_IN).

  • Property keys for nodes or relationships are not capitalized and can be camelCase (e.g. name, userID).

At this stage of creating your initial model, focus on the high-level design of your model, i.e. how your entities connect. The Define entities step describes a more detailed view on how to allocate certain information in a graph (e.g., as a node, relationship, property, etc).

Instance model

An instance model is a representation of the data that is stored and processed in the actual model. You can use an instance model to test against your use cases.

To create an instance model, you need to have some sample data and load it to a deployment of your choice. The current example is a small but representative dataset:

CREATE (Apollo13:Movie {title: 'Apollo 13', tmdbID: 568, released: '1995-06-30', imdbRating: 7.6, genres: ['Drama', 'Adventure', 'IMAX']})
CREATE (TomH:Person {name: 'Tom Hanks', tmdbID: 31, born: '1956-07-09'})
CREATE (MegR:Person {name: 'Meg Ryan', tmdbID: 5344, born: '1961-11-19'})
CREATE (DannyD:Person {name: 'Danny DeVito', tmdbID: 518, born: '1944-11-17'})
CREATE (JackN:Person {name: 'Jack Nicholson', tmdbID: 514, born: '1937-04-22'})
CREATE (SleeplessInSeattle:Movie {title: 'Sleepless in Seattle', tmdbID: 858, released: '1993-06-25', imdbRating: 6.8, genres: ['Comedy', 'Drama', 'Romance']})
CREATE (Hoffa:Movie {title: 'Hoffa', tmdbID: 10410, released: '1992-12-25', imdbRating: 6.6, genres: ['Crime', 'Drama']})

The data used here can be found in the Movies example dataset, also available in Browser and Aura guides. However, in order to practice data modeling, it is recommended that you add the data manually using Cypher.

Define entities

An instance model helps you preview how the data will be stored as nodes, relationships, and properties. The next step is to refine your model with more details.

Labels

The dominant nouns in your application use case are represented as nodes in your model and can be used as node labels. For example:

  • Which person acted in a movie?

  • How many users rated a movie?

The nodes in your initial model are thus Person, Movie, and User. Note that creating a model is an iterative process and, after refactoring, your model may look different.

Read more about labels in Graph database concepts.

Node properties

You can use node properties to:

Anchor (where to begin the query)
MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]-(m:Movie)
RETURN m
Traverse the graph (navigation)
MATCH (p:Person)-[:ACTED_IN]-(m:Movie {title: 'Apollo 13'})-[:RATED]-(u:User)
RETURN p,u
Return data from the query
MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]-(m:Movie)
RETURN m.title, m.released

With these properties, it is easier to visualize what you need from the graph to answer the use case questions. For example:

Use case Steps required Query example

Which people acted in a movie?

  • Retrieve a movie by its title.

  • Return the names of the actors.

MATCH (m:Movie {title:'Hoffa'})<-[r:ACTED_IN]-(p:Person)
RETURN p.name

Which person directed a movie?

  • Retrieve a movie by its title.

  • Return the name of the director.

MATCH (m:Movie {title:'Hoffa'})<-[r:DIRECTED]-(p:Person)
RETURN p.name