Using Neo4j graph database: part 1

Using Neo4j graph database: part 1

Let's keep getting the most useful web development tips from InternetDevels developers.
Here's the first blog post about Neo4j in which you will learn the basics.

Neo4j is the most popular graph database (NoSQL). Unlike the conventional MySQL or PostgreSQL, it doesn’t store the data as a table but as a graph, thus giving you the flexibility in handling connections between the nodes, and these connections are considered the most important.

When you should use a graph database (Neo4j)

Graph databases give you an advantage when working with the data for which the connections are very important, especially if you have to work with several different levels of connections.

IT giants, such as Google, Facebook, LinkedIn and PayPal, are actively using graph databases. This allows them to fully unleash the potential of connections. Graph databases are much faster at working with highly connected data than relational and NoSQL databases, and they also have built-in support for graph algorithms. 

Installing Neo4j (Ubuntu)

1) Download Neo4j Community Edition from the official site

2) Unpack

tar -xf neo4j-enterprise-2.3.1-unix.tar.gz

3) For your convenience, set an environment variable which specifies the path to the folder where we have unpacked Neo4j. To do this, we can add a line in the .bashrc file (this is optional).

export NEO4J_HOME=/opt/neo4j-community-2.3.1

where /opt/neo4j-community-2.3.1 is the folder to which we have unpacked Neo4j/

4) Run

$NEO4J_HOME/bin/neo4j console


sudo $NEO4J_HOME/bin/neo4j console

where $NEO4J_HOME is the variable we have set in (3). If we haven’t done that, we should specify the full path.

Neo4j can also be run as a service, with this command:

$NEO4J_HOME/bin/neo4j start

If you have done everything right, you’ll see the Neo4j admin menu at this link http://localhost:7474/browser/


Cypher is a query language used in Neo4j.

Here are some examples of queries:

MATCH (n)RETURN n LIMIT 100 — selection

MATCH (n {name:"p1"})


selection by name field

CREATE (n:Person {name:"p2"})


creation (the screen is similar to the previous one)

MATCH (n {name:"p2"})


removal (nothing is returned in this query, so there is no screen)

MATCH (a:Person),(b:Person)

WHERE = 'p1' AND = 'p2'

CREATE (a)-[r:RELTYPE]->(b)


setting a connection

MATCH (n:Person)


select by (Person) type

MATCH (node1)-[:RELTYPE]->(node2)-[:RELTYPE]->(node3)

RETURN node3

selection by connections (two levels of hierarchy)

MATCH (node1:Person { name:'p2' })-[r]->(node2:Person)


selection of all connections for the given node

MATCH (node1:Person { name:'p2' })-[r]->(node2:Person { name:'p3' }) SET r.distance = 10


setting a value for a given connection

MATCH (from:Person {name: 'p2'})-[:RELTYPE*1..100]->(to)

RETURN distinct to

selection of nodes with which you can reach the given node through a RELTYPE connection within 1-100 steps

MATCH p =(:Person { name: "p1" })-[:RELTYPE*0..5]-(:Person { name: "p5" }) RETURN extract(n IN nodes(p)| coalesce( AS `names`, length(p) ORDER BY length(p)


selection of paths in which you can go from one given point to another given point within 0-5 steps

MATCH (node1:Person { name:"p1" }),(node2:Person { name:"p5" }), p = shortestPath((node1)-[rels:RELTYPE*0..5]-(node2)) WHERE ALL (r IN rels WHERE NOT r.distance = 7)


finding the shortest path (here the shortest path is a path with the least number of intermediate nodes) that would meet the given requirements

MATCH p=(startNode:Person { name:"p1" })-[rels:RELTYPE*1..4]->(endNode:Person { name:"p5" }) RETURN p AS shortestPath, reduce(distance=0, r in rels | distance + r.distance) AS totalDistance


finding the shortest path between the given nodes (here the shortest path is the one for which the sum of the distance value for the RELTYPE connection is the smallest).

MATCH p=(startNode:Person { name:"p1" })-[rels:RELTYPE*1..4]->(endNode) WITH p, reduce(distance=0, r in rels | distance + r.distance) AS totalDistance WHERE totalDistance = 28


searching for all the routes that have the given sum of the distance value for the RELTYPE connection within 1-4 steps

As you can see, Cypher is a simple and yet flexible query language, adapted for graph data displays.

Here is a guide for Neo4j queries -

In the next part, we will put the above information into practice, and create an API using Neo4j as a database, Phalconphp as a framework, Neo4jphp as PHP wrapper for Neo4j and Redis for caching.

7 votes, Rating: 5

Read also


Creation of RTL CSS is quite an important process in ...


After the official release, more and more Drupal developers are...


Useful tips by the developers of our Drupal development company make social networks closer and your life easier ;)...


Hey there! If you are interested in Drupal web development, tips by our dev could do you a world of good. Welcome to...


In one of the recent blog posts by our developer, we offered you Drupal 8 development tips. Today, we'll...

Need a quote? Let's discuss the project

Are you looking for someone to help you with your Drupal Web Development needs? Let’s get in touch and discuss the requirements of your project. We would love to hear from you.

Join the people who have already subscribed!

Want to be aware of important and interesting things happening? We will inform you about new blog posts on Drupal development, design, QA testing and more, as well news about Drupal events.

No charge. Unsubscribe anytime