Let's say I have a directed graph with nodes of types A and B. Nodes of type A can be connected to nodes of type A or B, and nodes of type B have no outgoing connections.
Also, nodes B has a boolean
property. I need to find all nodes A that: have no direct or inderect connections to nodes B if node A has conection to at least 1 node B, it should be returned only if all connected nodes B has a property set to true.
Or in other words, I need to find all A which are not connected to any B with property set to false.
I was trying to do it with query:
OPTIONAL MATCH (a:A)-[*]->(b:B)
WITH a,b, collect(b) as bc
WITH a,b,COLLECT(bc) AS coll
UNWIND coll as unwinded
WITH a,b,unwinded
WHERE ALL (x IN unwinded WHERE x.prop = true)
return a
But it returns me A if they have at least 1 b with prop=true
. What am I doing wrong?
Thank you!
This should return all A
nodes that have no connected B
nodes with a false prop
:
MATCH (a:A)
OPTIONAL MATCH (a)-[*]->(b:B { prop: false })
WITH a, COLLECT(b) AS bs
WHERE SIZE(bs)= 0
RETURN a;
Here is a console showing this query.
[UPDATE]
As suggested by @InverseFalcon, the above query can be greatly simplified:
MATCH (a:A)
WHERE NOT (a)-[*]->(:B {prop:false})
RETURN a;
Does this work?
MATCH (a:A)
OPTIONAL MATCH (a)-[*]->(b:B {prop: false})
WITH a, b
WHERE b IS NULL
RETURN a
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.