New York
Join 1000+ graph enthusiasts & innovators October 23–24  •  Use code WEB10 for 10% off!


World's leading graph database, with native graph storage and processing.

Property graph model and Cypher query language makes it easy to understand.

Fast. Natural. Fun.

Test-Drive Neo4j with Cypher

Friends of Friends

Find all of Joe's second-degree friends


Joe knows Sally, and Sally knows Anna. Bob is excluded from the result because, in addition to being a 2nd-degree friend through Sally, he's also a first-degree friend.

 * To install Neo4j-PHP-Client, we use Composer
 * $ curl -sS | php
 * $ php composer.phar require graphaware/neo4j-php-client

require __DIR__.'/vendor/autoload.php';

use GraphAware\Neo4j\Client\ClientBuilder;

// change to your hostname, port, username, password
$neo4j_url = "bolt://neo4j:password@localhost";

// setup connection
$client = ClientBuilder::create()
    ->addConnection('default', $neo4j_url)

// setup data
$insert_query = <<<EOQ
UNWIND {pairs} as pair
MERGE (p1:Person {name:pair[0]})
MERGE (p2:Person {name:pair[1]})
MERGE (p1)-[:KNOWS]-(p2);

// friend data to insert
$data = [["Jim","Mike"],["Jim","Billy"],["Anna","Jim"],

// insert data
$client->run($insert_query, ["pairs" => $data]);

// friend of friend: query
$foaf_query = <<<EOQ
MATCH (person:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf)
WHERE = {name}
  AND NOT (person)-[:KNOWS]-(foaf)

// friend of friend: build and execute query
$params = ['name' => 'Joe'];
$result = $client->run($foaf_query, $params);

foreach ($result->records() as $record) {
  echo $record->get('name') . PHP_EOL;

// common friends: query
$common_friends_query = <<<EOQ
MATCH (user:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf:Person)
WHERE = {user} AND = {foaf}
RETURN AS friend

// common friends: build and execute query
$params = ['user' => 'Joe', 'foaf' => 'Sally'];
$result = $client->run($common_friends_query, $params);

foreach ($result->records() as $record) {
  echo $record->get('friend') . PHP_EOL;

// connecting paths: query
$connecting_paths_query = <<<EOQ
MATCH path = shortestPath((p1:Person)-[:KNOWS*..6]-(p2:Person))
WHERE = {name1} AND = {name2}
RETURN [n IN nodes(path) |] as names

// connecting paths: build and execute query
$params = ['name1' => 'Joe', 'name2' => 'Billy'];
$result = $client->run($connecting_paths_query, $params);

foreach ($result->records() as $record) {

Downloading and Installing PHP

  1. Install Composer from within web directory: $ curl -sS | php
  2. Install Neo4j-PHP-Client from within web directory: $ php composer.phar require graphaware/neo4j-php-client
  3. Copy and paste code at left into php file and run

Intro to Graph Databases

  • Play Video
    Intro to Graph Databases Episode #1 - Evolution of DBs
  • Play Video
    Intro to Graph Databases Episode #2 - Properties of Graph DBs & Use Cases
  • Play Video
    Intro to Graph Databases Episode #3 - Property Graph Model

Training and Certification

Why Neo4j

Whiteboard Friendly

The real world is a graph of relationships — not tables of columns and rows.

Global Community

Support for popular languages & frameworks. Learn from books, meetups, and trainings.

Native Graph Performance

Optimized graph storage and processing with the expressive Cypher query language.

What It's Made of

Flexible Schema


Clustering for HA and Performance

Powerful Cypher Query Language

Neo4j Browser

Drivers for Popular Languages and Frameworks

Data Import


Upcoming Events Near You


    • Play Video
      Neo4j's Engineering Evolution by Chief Scientist Dr. Jim Webber
    • Play Video
      Better Data, Better Decisions – 1000x Faster with Neo4j.
    • Play Video
      The Best Thing about Graph Databases

    Neo4j in the Social Universe

    44.3k public Slack channel messages
    3.2k GitHub repos about Neo4j
    @graphileon: Visualizing a bill of materials in Neo4j , including component change history @Neo4j
    1,638 developers have answered StackOverflow questions about Neo4j
    7430 topics on Google Groups

    GraphGist Projects