How to get the first node from relationship with property value?

Hi guys!
I'am looking for a solution.
Here's the graph

create (n:Node{id:1,closed:false})
create (n)-[:PARENT]->(n1:Node{id:2, closed:true})-[:CHILD]->(n)
create (n1)-[:PARENT]->(n2:Node{id:3, closed:false})-[:CHILD]->(n1)
create (n2)-[:PARENT]->(n3:Node{id:4, closed:true})-[:CHILD]->(n2)
create (n3)-[:PARENT]->(n4:Node{id:5, closed:false})-[:CHILD]->(n3)
create (n4)-[:PARENT]->(n5:Node{id:6, closed:false})-[:CHILD]->(n4)

I need to get the first node from relationship with property

closed = true

I came up with this query, it works for me but I feel that there is better solution for this because I don't need to scan all the nodes above. LIMIT not works here, because I want to change this node after I find it inside the transaction.
I don't know how much nodes above with closed = false.

match v=(p:Node{id:6})-[:CHILD*]->(:Node{closed:true}) 
with nodes(v) as nodes
UNWIND nodes AS node
with node where node.closed = true
with collect(node)[0] as first
return first

Thanks for your help!

Hi @alexander_kaluz !

I swear i'll stop answering if you don't fix your model :wink:

So, I have a bad news for you.You can't avoid this overhead of search with plain cypher. If you really have strong stop/go conditions on that property. Do this:

create (n:Node{id:1,closed:false})
create (n)-[:PARENT]->(n1:Node{id:2, closed:true})
create (n1)-[:PARENT]->(n2:Node{id:3, closed:false})
create (n2)-[:PARENT]->(n3:Node{id:4, closed:true})
create (n3)-[:PARENT]->(n4:Node{id:5, closed:false})
create (n4)-[:PARENT]->(n5:Node{id:6, closed:false})

MATCH (n:Node)
where n.closed = true

match (p:Node{id:6})
CALL apoc.path.expandConfig(p, {
	relationshipFilter: "<PARENT",
    labelFilter: "/CLOSED"
yield path 
return last(nodes(path))


PS: Welcome to Wonderland

Well, i thought that it's possible. You mean I should add additional information in the node to make traversal easier.
Thanks for helping me. I will check out other solutions but i thought that for group tree this model of data will be enough


Your query is fine, it's just that in a big graph will start giving poor performance. Data Modeling and Performance strongly connected on GraphDbs. Normal queries with Cypher will collect all that data and then apply the filter you are requesting.

As you noticed, in your use case, you may end traversing much more paths hat you really need. APOC path procedures will help you with that, but, their logic is Label/Relationships centric. Don't be afraid of adding labels or differ t relations to your model if needed.