Neo4j Samples (Simple and Complex Queries)

In this post, we will go through examples of neo4j simple and complex queries.
We will go through some neo4j samples and some scenarios. Let’s begin with the basics.
Create Nodes with properties:

Query To Create Simple Nodes:

Above query will create nodes of type Actor.

Query To add a new property to existing Node:

MATCH (actor:Actor) SET actor.friendsCount = 0 RETURN actor;

Above query will simply add property friendsCount to all Actor nodes.

Create Relationship between Nodes in Neo4j:

Assuming that you all are familiar with Twitter and Instagram where you follow people of your choice, the relationship “follows” ( i.e. for e.g one actor following other actor) has properties

FollowingStatus: (pending, approve, rejected)

Let take an example, Drew Barrymore starts following Paris Hilton, so she just sends a request.

Above query can get you in trouble because whenever you execute it, it will create multiple relations between the same node, and can cause confusion.  To solve this we can use UNIQUE keyword as shown below.

Suppose, on top of this you also want to log the information like whenever you try to update the same relationship you just want to log a timestamp or a flag. For that we can use the below query.

Suppose, if you want to update the status value only, considering that Paris Hilton has accepted here follow request.

Above query will update the Follow request status property from pending to accepted if and only if it is pending. And at the same time will also update the friendscount of both the node.
One can also make use of CASE feature of Neo4j as seen below.

Conditional update:

The above query works the same way as the previous one, but there are many scenarios in which the above query can help you like adding multiple case scenarios.
Note: Don’t forget to put END after case.

Data Retrieval in Neo4j

By using above scenario, lets create
nodes of Movie with property “name”( name of the movie) and “likes”(number of likes).

An actor can have LIKE relation with node Movie. This scenario can be represented using the Figure below.


Blue nodes are Actors and Orange ones are Movies.

37 (Actor Node): Drew Barrymore
46 (Actor Node): Paris Hilton
67 (Movie Node): ET

rest of nodes are other movies that are liked by Paris Hilton.

Basic Retrieval of Nodes by its Property:

We can clearly see that the output of the above query will be the node of Drew Barrymore.

One more example. This query will output all the actor nodes with Age greater than 19.

Normally, you won’t find any trouble in above query, but in real word, there will be a need of some complex retrieval combining properties and node status.

Complex Retrieval of Nodes by its Property:

Suppose, you want to suggest your actor some moview.

In our data lets assume that Drew Barrymore is following (Approved) Paris Hilton.
Drew Barrymore has already liked “ET” movie and Paris Hilton has liked “ET”, “Grown Ups”, “The Grudge”, “House Of Wax”.

Now, you want data to be like,

NAME_OF_MOVIE          |              WATCHED           |              NO_OF_LIKES

Grown Ups                                 NOT WATCHED                                10

Matrix                                          NOT WATCHED                                3

The Grudge                                 NOT WATCHED                                 2

ET                                                 WATCHED                                         1

House Of Wax                            NOT WATCHED                                  1

To fetch data as shown above, one can use example query like the following.

Output of which is,


Thank you for reading and please feel free to comment if you encounter any difficulties or specific scenarios.

Also if you like to set up High Availability Cluster of Neo4j for your organization or personal use please have a look at

Leave a Reply

Your email address will not be published. Required fields are marked *