What is Neo4j?

What is a graph

Neo4j is a graph database that stores data in a graph. Data is stored as nodes and relationships instead of tables or documents.

Graph databases are particularly useful when the connections between data are as important as the data itself.

In this lesson, you will learn about graphs and the elements that make up a Neo4j graph database.

A graph shows how objects are related to each other.

A large graph showing circles connected to each other by lines

The objects are referred to as nodes (vertices) connected by relationships (edges).

Neo4j uses the graph structure to store data and is known as a labeled property graph.

Nodes, Relationships, Labels, and Properties

Data within Neo4j is stored and organized using:

  • Nodes

  • Relationships

  • Labels

  • Properties

Nodes

Nodes are the circles in a graph. Nodes typically represent objects or entities.

Imagine a social network, the entities (e.g. people, locations, companies) would be represented by nodes.

Three nodes representing Michael

Each entity would be would be stored as a separate node in the graph.

Labels

Nodes are grouped by or categorized using labels. Labels describe what the nodes are, for example, Person, Company, Location.

Michael and Sarah nodes with a Person label

Nodes of the same type would have the same label.

Labels allow you to distinguish between different types of nodes and filter the graph.

Nodes can have multiple labels, for example, Michael is a Person and could also be an Employee.

Michael nodes with 2 labels

Use nouns for labels

Nodes typically represent things, and should be given a singular noun label. For example, Product, Event, Account.

Relationships

Relationships are the lines in the graph. Relationships describe how nodes within the graph are connected to each other.

Displaying a WORKS_AT relationship between Michael and Neo4j and a FOUNDED_IN relationship between Neo4j and Sweden

A relationship in Neo4j connects two nodes, referred to as the start and end nodes.

All relationships have:

  • a type - WORKS_AT, FOUNDED_IN.

  • a direction - Michael WORKS_AT Neo4j, Neo4j doesn’t work at Michael.

Nodes can have multiple relationships to other nodes.

Multiple relationships can be used to describe bi-directional relationships.

Michael and Sarah connected by 2 MARRIED_TO relationship Michael to Sarah and Sarah to Michael

Use verbs for relationship types

You can use a relationship to represent:

  • A personal connection - Person KNOWS Person, Person MARRIED_TO Person.

  • A fact - Person LIVES_IN Location, Person OWNS Car, Person RATED Movie.

  • A hierarchy - Parent PARENT_OF Child, Software DEPENDS_ON Library.

  • Any type of connection between 2 entities - Entity CONNECTED_TO Entity.

Properties

You can store data against nodes and relationships as properties.

Properties are named key, value pairs; for example firstName, lastName, and position.

Properties shown against nodes and relationships

Nodes and relationships can have any number of properties, and those of the same type do not have to have the same properties (i.e. Neo4j is schemaless).

Properties have a type (integer, boolean, string, list, etc) and can be unique identifiers (keys) for specific node labels.

Check your understanding

Data in Neo4j

Data within Neo4j is stored and organized using which of the following? (Select all that apply)

  • ✓ Nodes

  • ✓ Relationships

  • ✓ Labels

  • ✓ Properties

Hint

Nodes and relationships are the fundamental elements of a graph database. Labels are used to categorize nodes, and properties are used to store data within nodes and relationships.

Solution

Data within Neo4j is stored and organized using nodes, relationships, labels, and properties.

Summary

In this lesson, you learned that Neo4j stores and organizes data as a graph using nodes, labels, relationship, and properties.

In the next lesson, you will learn how graph thinking can help you solve problems.

Next, you will learn about some common use cases for graphs.