hellowong
hellowong

Reputation: 129

Can't create the relationship in Neo4j

Try to create the relationship in neo4j, but failed. I have 2 nodes, server and virtualmachine. Now i want to create the relationship between these 2 nodes.

MATCH (s:Server {name:"DHAAPP01"})
RETURN  s.name;
MATCH (v:VirtualMachine {virtualhost:"DHAAPP01"})
RETURN  v.name,v.virtualhost;

Then I tried to create the relationship with create command

CREATE (v:VirtualMachine)-[r:DEPENDS]->(s:Server)

Even the relationship created, it still cant show the relationship in the graphDB. I also tried to match 2 modes with the commands, but failed too.

MATCH (v:VirtualMachine)-[r:DEPENDS]-(s:Server)
WHERE v.name= 'DHA'
AND s.name = 'DHAAPP01'
RETURN v.name, v:VirtualMachine;

Would like your help and advise anything wrong in my command? Thanks for your help

Upvotes: 2

Views: 1944

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30417

You may have inadvertently created a new :VirtualMachine and new :Server node. That's what your first CREATE command did, and I'm pretty sure that's not what you wanted.

For creating a relationship between specific nodes, just match to them and create the relationship between them using the variables in the match, and if you need them returned, return the variables, including the variable on the relationship:

MATCH (s:Server {name:"DHAAPP01"})
MATCH (v:VirtualMachine {virtualhost:"DHAAPP01"})
MERGE (v)-[r:DEPENDS]->(s)
RETURN v, r, s

Upvotes: 1

Related Questions