Search code examples
memgraphdb

Why would one reference the node on itself?


I get why would someone use code like this

CREATE (a:Node {name: "Node A"})-[:RELATIONSHIP_TYPE]->(b:Node {name: "Node B"})-[:RELATIONSHIP_TYPE]->(c:Node {name: "Node C"})
CREATE (c)-[:RELATIONSHIP_TYPE]->(a)

I don't understand when would someone use self-reference form node to node itself

CREATE (a:Node {name: "Node A"})-[:RELATIONSHIP_TYPE]->(a)

What would be some real world scenario for this?


Solution

  • A self-reference in a graph database can be useful in certain scenarios where an entity has a relationship with itself. Social networks is the first scenario that comes to my mind. User might want to follow their own updates or activities.

    You could represent this self-following relationship with a self-reference. Here's an example of such query:

    CREATE (u:User {name: "Alice"})-[:FOLLOWS]->(u)
    

    You have one node (User Alice) with the label User and a property name set to "Alice". On top of it you have a relationship of type FOLLOWS from User Alice to itself, representing that Alice follows her own updates.