Neo4j Operators

Create a graph database for the following User Data

 
  name : satish 
  address:vellore
  salary :10000
  phone: 87777
  worksfor - VIT from 2020 March 21
  College - CEG
  school - KPC 
  knows-chris and ram
  
  name : ram
  address:chennai
  salary :5000
  phone: 98877
  worksfor - VIT from 2015 April 1
  college - CEG
  school - kpc
  knows - chris
  
  name : chris 
  address:chennai
  salary :20000
  phone: 4134
  worksfor - VIT from 2017 June 2 
  college - IIT
  school - kpc
  knows-tom
  
  name : Jeff 
  address:vellore
  salary :4000
  phone: 2344
  worksfor - Microsoft from 2021 March 21
  college - IIT
  school - kmk
  knows-tom
  
  name : tom
  address:chennai
  salary :34000
  phone: 5434
  worksfor Microsoft from 2021 August 4 
  college - NIT 
  school - kmk 
  knows - satish

The solution is given below


Create a node for every User

 
  create(u:user{name:'satish',address:'vellore',salary:10000,phone:8777})
  create(u:user{name:'ram',address:'chennai',salary:5000,phone:98877})
  create(u:user{name:'chris',address:'chennai',salary:20000,phone:4134})
  create(u:user{name:'jeff',address:'vellore',salary:4000,phone:2344})


Create a Node for every Organization

 
  create(o:organization{name:"VIT"})
  create(o:organization{name:"Microsoft"})

 

Create a node for every College

 
  create(c:college{name:"ceg"})
  create(c:college{name:"NIT"})
  create(c:college{name:"IIT"})


Create a node for every School

 
  create(s:school{name:"kpc"})
  create(s:school{name:"kmk"})


Creating Multiple Nodes with Properties

 
create (u:user{name:'satish'}),(u:user{name:"ram"}),(u:user{name:'tom'}) return m,c,o


Returning all Created Nodes

 
Match(n) return (n)

Create worksfor Relationships between Nodes

 
  match(u:user{name:'satish'}),(o:organization{name:'VIT'}) create (u)-[w:worksfor{date:date('2020-03-21')}]->(o) return u,w,o;
  match(u:user{name:'ram'}),(o:organization{name:'VIT'}) create (u)-[w:worksfor{date:date('2015-04-1')}]->(o) return u,w,o;
  match(u:user{name:'chris'}),(o:organization{name:'VIT'}) create (u)-[w:worksfor{date:date('2017-06-2')}]->(o) return u,w,o;
  match(u:user{name:'jeff'}),(o:organization{name:'Microsoft'}) create (u)-[w:worksfor{date:date('2021-03-21')}]->(o) return u,w,o;
  match(u:user{name:'tom'}),(o:organization{name:'Microsoft'}) create (u)-[w:worksfor{date:date('2021-08-04')}]->(o) return u,w,o;


Create College Relationships between users and college Node

 
  match(u:user{name:'satish'}),(c:college{name:'ceg'}) create (u)-[s:studied]->(c) return u,s,c;
  match(u:user{name:'ram'}),(c:college{name:'ceg'}) create (u)-[s:studied]->(c) return u,s,c;
  match(u:user{name:'jeff'}),(c:college{name:'IIT'}) create (u)-[s:studied]->(c) return u,s,c;
  match(u:user{name:'chris'}),(c:college{name:'IIT'}) create (u)-[s:studied]->(c) return u,s,c;
  match(u:user{name:'tom'}),(c:college{name:'NIT'}) create (u)-[s:studied]->(c) return u,s,c;


Create School Relationship between users and school

 
  match(u:user{name:'satish'}),(s:school{name:'kpc'}) create (u)-[a:attended]->(s) return u,a,s;
  match(u:user{name:'ram'}),(s:school{name:'kpc'}) create (u)-[a:attended]->(s) return u,a,s;
  match(u:user{name:'chris'}),(s:school{name:'kpc'}) create (u)-[a:attended]->(s) return u,a,s;
  match(u:user{name:'jeff'}),(s:school{name:'kmk'}) create (u)-[a:attended]->(s) return u,a,s;
  match(u:user{name:'tom'}),(s:school{name:'kmk'}) create (u)-[a:attended]->(s) return u,a,s;


Create knows relationship between users

 
  match(u:user{name:'satish'}),(u1:user{name:'chris'}) create (u)-[k:knows]->(u1) return u,k,u1;
  match(u:user{name:'satish'}),(u1:user{name:'ram'}) create (u)-[k:knows]->(u1) return u,k,u1;
  match(u:user{name:'ram'}),(u1:user{name:'chris'}) create (u)-[k:knows]->(u1) return u,k,u1;
  match(u:user{name:'chris'}),(u1:user{name:'tom'}) create (u)-[k:knows]->(u1) return u,k,u1;
  match(u:user{name:'jeff'}),(u1:user{name:'tom'}) create (u)-[k:knows]->(u1) return u,k,u1;
  match(u:user{name:'tom'}),(u1:user{name:'satish'}) create (u)-[k:knows]->(u1) return u,k,u1;


Select Operation on the Database

 
  Get the names of all users in the database
match(u:user) return u.name;


Get name property of any two specific nodes

 
match(m:manager),(c:clerk) return m.name,c.name


Use of in operator

 
match(u:user) where u.address in ['vellore','mumbai'] return u


Use of or operator

 
match(u:user) where u.address ='vellore' or u.address='mumbai' return u 


Use of and operator

 
match(u:user) where u.address ='vellore' and  u.salary=10000 return u 

Add a property for an existing node (setting school property with the value scope for the user node)

 
Set school as scope for User Tom
match(u:user{name:'tom'}) set u.school='scope' return u

Remove school property for a user node by name Tom

 
match(u:user{name:'tom'}) remove u.school return u


Delete a specific node

 
create(u:user{name:'test'}) return u
match(u:user{name:'test'}) delete u


Remove a label and set a new label for a node

 
create(u:user{name:'test'}) return u
match(u:user{name:'test'}) remove u:user set u:admin return u


Update mulitple properties for a node

 
create(u:user{name:'test',salary:5000}) return u
match(u:user{name:'test'}) set u.name='test1', u.salary=3000 return u