Search code examples
neo4jcypherpy2neo

neo4j get parent of a node of a given id


I have this Neo4J database and I have the id of the nodes of interest. I need to find the parent of those nodes. How can I do this? I am using py2neo and I have a neo4j object.

Problem is I don't know Cypher at all and not even Gremlin. What cypher query will give me the result?


Solution

  • Since you are not sharing your data model I'm assuming you have nodes with :PARENT_OF relationship between them. Something like (:Node)-[:PARENT_OF]->(:Node).

    If you have the internal id of the node of interest, you should use the id() function:

    MATCH (nodeOfInterest:Node)<-[:PARENT_OF]-(parent:Parent)
    WHERE id(nodeOfInterest) = 10
    RETURN parent
    

    If the id you have is a property, then you can directly use it in the pattern matching:

    MATCH (nodeOfInterest:Node {id : 10})<-[:PARENT_OF]-(parent:Parent)
    RETURN parent
    

    I suggest you to take a look in these links: