The property graph model

Neo4j · Essentials · 10 min

Every Cypher query and data model is built from these four ingredients, so this is the mental model for everything else.

Neo4j stores a property graph:

  • Nodes are the entities: a person, a product, an account. A node can have zero or more labels such as :Person or :Customer that group nodes into roles.
  • Relationships connect two nodes. Each has exactly one type (:FOLLOWS, :BOUGHT), a direction from a start node to an end node, and always has both ends; there are no dangling relationships.
  • Properties are key/value pairs on nodes and relationships: name: 'Ann', since: 2021. Values are strings, numbers, booleans, temporal and spatial types, or lists of those. There are no nested maps as property values.

Compared with a relational database, relationships are stored data, not computed at query time with joins. Following a relationship costs the same whether the database holds a thousand or a billion nodes, which is why multi-hop questions ("friends of friends who bought this") stay fast.

Neo4j is schema-optional: you can add labels, properties and relationship types as you go, and add constraints later.

To try it, create a free Neo4j Aura instance in the cloud, install Neo4j Desktop, or run docker run -p 7474:7474 -p 7687:7687 neo4j. Open Neo4j Browser at http://localhost:7474 to run Cypher and see results drawn as a graph; applications connect over the Bolt protocol on port 7687.

Example

// A tiny graph, written as Cypher patterns:
//
//   (:Person {name: 'Ann'})-[:FOLLOWS {since: 2021}]->(:Person {name: 'Bob'})
//   (:Person {name: 'Bob'})-[:WORKS_AT]->(:Company {name: 'Acme'})
//
// nodes:          (variable:Label {property: value})
// relationships:  -[variable:TYPE {property: value}]->
//
// Ann  --FOLLOWS-->  Bob  --WORKS_AT-->  Acme
RETURN 'nodes, relationships, labels, types, properties' AS model;

Remember

  • Nodes have labels; relationships have exactly one type and a direction
  • Both nodes and relationships carry properties
  • Relationships are stored, so traversals do not need joins
  • Use Aura Free, Desktop or Docker plus Neo4j Browser to start

Watch out for

  • Storing a relationship as a foreign-key-like property (author_id) instead of a real relationship.

Check yourself

  1. Which statement about Neo4j relationships is true?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: neo4j.com/docs/getting-started/graph-database/

Builds on

Nothing: this is a foundation.

Unlocks