Skip to the content.

Neo4j

Information

Introduction

Neo4j is a high-performance, NoSQL graph database management system. It is designed to store and process data in a graph format, using nodes, relationships, and properties. Unlike traditional relational databases, Neo4j excels at representing complex, interconnected data, making it ideal for use cases like social networks, recommendation engines, fraud detection, and knowledge graphs.

Key Features

Installation

Docker

For local development, the easiest way to run Neo4j is using Docker.

docker run \
    --name neo4j \
    -p 7474:7474 -p 7687:7687 \
    -d \
    -v $HOME/neo4j/data:/data \
    -v $HOME/neo4j/logs:/logs \
    -v $HOME/neo4j/import:/import \
    -v $HOME/neo4j/plugins:/plugins \
    --env NEO4J_AUTH=neo4j/testpassword \
    neo4j:latest

Docker Compose

Alternatively, use a docker-compose.yml file:

services:
    neo4j:
        image: neo4j:latest
        container_name: neo4j
        ports:
            - "7474:7474"
            - "7687:7687"
        volumes:
            - ./neo4j/data:/data
            - ./neo4j/logs:/logs
            - ./neo4j/import:/import
            - ./neo4j/plugins:/plugins
        environment:
            NEO4J_AUTH: neo4j/testpassword
            NEO4J_PLUGINS: '["apoc"]' # Optional: enable APOC library

Usage, tips and tricks

CREATE (alice:Person {name: 'Alice', age: 30, city: 'New York'})
CREATE (bob:Person {name: 'Bob', age: 25, city: 'Los Angeles'})
CREATE
  (carol:Person {name: 'Carol', age: 27, city: 'Chicago'}),
  (dave:Person {name: 'Dave', age: 35, city: 'Miami'})
CREATE
  (neo:Movie {title: 'The Matrix', year: 1999}),
  (trinity:Person {name: 'Trinity', age: 29})
CREATE
  (alice:Person {name: 'Alice'}),
  (bob:Person {name: 'Bob'}),
  (alice)-[:FRIEND]->(bob)
MERGE (alice:Person {name: 'Alice'})
ON CREATE SET alice.age = 30, alice.city = 'New York'
// Create People
CREATE
  (alice:Person {name: 'Alice', age: 30, city: 'New York'}),
  (bob:Person {name: 'Bob', age: 25, city: 'Los Angeles'}),
  (carol:Person {name: 'Carol', age: 27, city: 'Chicago'})

// Create Movies
CREATE
  (matrix:Movie {title: 'The Matrix', year: 1999}),
  (inception:Movie {title: 'Inception', year: 2010})

// Create Relationships
CREATE
  (alice)-[:FRIEND]->(bob),
  (bob)-[:FRIEND]->(carol),
  (alice)-[:WATCHED]->(matrix),
  (bob)-[:WATCHED]->(matrix),
  (carol)-[:WATCHED]->(inception)
MATCH (alice:Person {name: 'Alice'})-[:FRIEND]->(friends)
RETURN friends.name, friends.age, friends.city
MATCH (person:Person)-[:WATCHED]->(movie:Movie {title: 'The Matrix'})
RETURN person.name, person.age
MATCH (alice:Person {name: 'Alice'})-[:FRIEND]->()-[:FRIEND]->(fof)
RETURN DISTINCT fof.name
MATCH (person:Person)-[:WATCHED]->(movie:Movie)
RETURN person.name AS Person, collect(movie.title) AS MoviesWatched
MATCH (n)
DETACH DELETE n

See also