Adding relationship to existing nodes with Cypher

Viewed 65031

I'm trying out Neo4j for the first time. I'm using the 2.0-RC1 community edition.

I've created some nodes:

MERGE (u:User{username:'admin',password:'admin'})
MERGE (r1:Role{name:'ROLE_ADMIN'})
MERGE (r2:Role{name:'ROLE_WEB_USER'})
MERGE (r3:Role{name:'ROLE_REST_USER'})

and now I want to add relationships between the nodes. However, I don't want to clear out the existing database created with the script above, add the statements and run it again. I want to add relationships to the existing nodes. Google helped me find this:

START n=node(*), m=node(*)  
where has(n.username) and has(m.name) and n.username = 'admin' 
and m.name = 'ROLE_WEB_USER' 
create (n)-[:HAS_ROLE]->(m)

Which works fine (even though I don't understand all the syntax). However, I am aware that this finds any node with a username property and any node with a name property, instead of using labels to check that it has the right type of node.

How can I do the same using labels?

2 Answers

Update as of 4/2020:
The new Cypher syntax is as follows. as 'CREATE INDEX ON' has been deprecated is..

CREATE INDEX FOR (n:Label) ON (n.property)
Related