Ask Your Question
0

How can a Neo4J database be queried recursively for a cypher?

asked 2022-04-21 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-04-30 14:00:00 +0000

lakamha gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2022-04-21 11:00:00 +0000

Seen: 18 times

Last updated: Apr 30 '22