A Neo4J database can be queried recursively using a Cypher query by leveraging the MATCH
clause in combination with the WITH
and UNION
clauses.
Here is an example Cypher query that recursively queries a Neo4J database to find all nodes that are related to a specific node:
MATCH (node)-[:RELATED_TO*]->(related_node)
WHERE ID(node) = {node_id}
WITH collect(related_node) AS related_nodes
UNWIND related_nodes AS related_node
MATCH (related_node)-[:HAS_PROPERTY]->(property)
RETURN related_node, property.name
In this query, we use the MATCH
clause to find all nodes that are related to a specific node, denoted by {node_id}
. The *
symbol in the relationship pattern [:RELATED_TO*]
represents a variable-length relationship that can be traversed recursively.
We then use the WITH
clause to collect all the related nodes into a list and then UNWIND
them to iterate over each related node individually. We use another MATCH
clause to find all properties of each related node and finally return both the related node and its properties.
This query would return all nodes related to the specified node and their corresponding properties in a tabular format.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-04-21 11:00:00 +0000
Seen: 18 times
Last updated: Apr 30 '22
Is there support for Gremlin in Neo4J?
Can the execution plan in neo4j be modified based on the limit of the return results?
What does the error message "Invalid input: [, expected '+' or '-'" mean in neo4j?
How can large graphml files be filtered by removing certain portions?
What could be the reason for the invalid connection to neo4j in Android Studio?