Neo4j Create, Match, Order By

Create a node and add the relationship

 
  create (d:dept) return d
  match(e:employee),(d:dept) create (e)-[w:worksfor]-> (d) return e,w,d



Add date for this worksfor relationship

 
  match(e:employee{name:'satish'})-[w:worksfor]->(d:dept{name:'scope'}) set w.dateofjoining = date('2019-4-20') return e,w,d


Bring all nodes that participate in the worksfor relationship

 
  match(e)-[w:worksfor]->(d) return e ,w,d

 

Deleting a node

 
  create (t:test) return t
  match(t:test) delete t


Deleting a node using a specific property

 
  create (t:test{name:'satish'}) return t
  match(t:test{name:'satish'}) delete t


Deleting a node using the id

 
  create (m),(n) return m,n
  match (n) where id(n) =3 return n
  match (n) where id(n) =3 delete n
  match (n) where id(n)=2 delete n
  match (n) where id(n) in [2,3] delete n


NOTE - You cannot delete a node in a relationship directly. First you must detach and delete. Only when the node is detached from the relationship it can be deleted as shown below.

 
  match(d:dept) detach delete d


Delete the worksfor relationship between two specific nodes

 
  match(e:employee{name:'satish'})-[w:worksfor]->(d:dept{name:'scope'}) delete w 


Delete a relationship using Id

 
  match(e:employee{name:'satish'}),(d:dept{name:'scope'}) create(e)-[w:worksfor]->(d) return e,w,d
  match(e)-[w:worksfor]->(d) where id(w)=2 delete w

Create another relationship manages between employee and department

 
  match(e:employee{name:'satish'}),(d:dept{name:'scope'}) create (e)-[w:worksfor]->(d) return e,w,d
  match(e:employee{name:'satish'}),(d:dept{name:'scope'}) create (e)-[m:manages]->(d) return e,m,d

Match all nodes that participate in either worksfor or manages relationship

 
  match (e)-[w:worksfor|manages]->(d) return e,w,d