Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to link a node to itself in Neo4J?

Tags:

neo4j

The specifics of my app requires that a node can be linked to itself. Can it be done in Neo4J? Can a new edge be created between the node and itself using the CREATE statement? Will I then be able to obtain it using the MATCH query?

like image 755
Aerodynamika Avatar asked Dec 15 '14 19:12

Aerodynamika


1 Answers

Yes, it can be done.

Citation from the docs:

While relationships always have a direction, you can ignore the direction where it is not useful in your application.

Note that a node can have relationships to itself as well

You can create these relationships just like any others.

CREATE (p:Person { name: "Sam" });
MATCH (p:Person { name: "Sam" })
MERGE (p)-[:knows]->(p);

Although, for obvious reasons, the directionality of a relationship becomes way less interesting if you're pointing a node to itself because there's no difference between the head and the tail.

like image 176
FrobberOfBits Avatar answered Sep 21 '22 04:09

FrobberOfBits