Neo4j Query Tutorial – MATCH and MERGE

Neo4j Query Tutorial – MATCH and MERGE

Example 1:

Match two nodes and create a relationship between them.

MATCH (person1:LABEL{id:'1234'}),(person2:LABEL{id:'5678'})
MERGE (person1)-[r:FRIEND]-(person2)

Example 2:

Match two nodes and create/update a relation between them. Optionally set properties to the nodes or relationships with ON CREATE SET or ON MATCH SET. Use r+={property:’value’} to add properties or update existing properties.

MATCH (user1:USER{id:'123'}),(user2:USER{id:'456'}) 
MERGE (user1)-[r:FRIEND]-(user2)
ON CREATE SET
    r={createdDate:'01-04-2016',updatedDate:'01-04-2016'}
ON MATCH SET 
    r={createdDate:'01-04-2016',updatedDate:'01-04-2016'}
RETURN user1,user2

Example 3:

MATCH a node and pass the required node or relationship to next level of processing using WITH option.

MATCH (user1:USER{id:'123'})
WITH user1
MATCH (user2:USER{id:'456'})
WITH user1,user2
MERGE (user1)-[r:FRIEND]-(user2)
ON CREATE SET
    r={createdDate:'01-04-2016',updatedDate:'01-04-2016'}
ON MATCH SET 
    r={createdDate:'01-04-2016',updatedDate:'01-04-2016'}
RETURN user1,user2,r

Example 4:

Update out going releationships for a node

MATCH (user1:USER{id:'123'})-[r]-()
SET 
    r.property='value'
RETURN user1

Example 5:

Use a property value of a one object/node/relationship with another object/node/relationship.

MATCH (user1:LABEL{id:'123'})-[r]-(user2:LABEL{property:user1.propertyValue})
SET 
   r={someProperty:'value'}
RETURN user1.id,r.id,user2.id

Example 6:

MATCH (n:LABEL) WHERE n.property='value'
RETURN n

[ YOU MAY ALSO LIKE ]

One thought on “Neo4j Query Tutorial – MATCH and MERGE

  1. saki

    nice blog too informative. looking and reading your points its so impressive. doing more blog like this. i really appreciated doing like this.

    Reply

Leave a Reply