cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Return a node only if all relationship connected to it are activated

vence_andersen1
Graph Buddy

Hi,


Reference to the above image, nodes on the left are "Label_1", nodes on the right are "Label_2". On Label_2, the node which has the properth "F"(the middle node on right) has 2 relationships connected to it. We can return that node by either querying the nodes that has the property "B" or propery "C".

Is it possible to return that node with property "F" only if both the nodes are connected to it(the nodes that has the property "B" and propery "C") are queried?

Thanks in advance.

5 REPLIES 5

jo_nathan
Node Clone

sure

match(nodeA:Label_1{name:"A"})-[:has_option]->(nodeF:Label_2{name:"F"})<-[:has_option]-(nodeC:Label_1{name:"C"})
return nodeF;

alternatively:

match(nodeA:Label_1{name:"A"})-[:has_option]->(nodeF:Label_2{name:"F"})
match (nodeF)<-[:has_option]-(nodeC:Label_1{name:"C"})
return nodeF;

Using MATCH, only exact matches to the pattern will be returned.
There is OPTIONAL MATCH for ... optional patterns.

When searching on properties, make sure you have created indices on these properties!

Thanks for your reply. We can also return the nodeF by simply querying either of the node B or C. But I wanted the nodeF to be returned "only" if both nodeB and C are queried.

I don't want nodeF to be returned here.

Hey again.
I am not sure I understand your use-case. Could you elaborate in more detail?
Also what do these Relationships represent?

The query I posted above will return node F only when both "A" and "C" connect to it. The query matches the exact pattern that both "A" and "C" connect to "F".

Does this linked post refer to the same use-case?

Am I understanding correctly:

When querying patterns, you want nodes only to be returned, if all of their ingoing relationships (relationships to the node) have been queried? The number of ingoing relationships might vary. Only if all of them are part of the pattern, your query will return the node.

Yes it is.

Well sorry reference to the main post, it's nodes "B" and "C".

Exactly. Here nodeF can be returned by querying either nodeB "OR" nodeC, but I want it to returned only if both nodesB "AND" nodeC are queried.

I made a generalized version of the query above.
If you have control over the queries that go to the database, you can wrap your query in the following way.
This feels very hacky and I don't like it.

match(targetnode:Label_2{name:"F"})<-[r]-()
with count(r) as countrelationships,targetnode
match(sourcenode:Label_1)-[q]->(targetnode)
// Put all the connected nodes you want to query here
where sourcenode.name="B"  or sourcenode.name="C"
// 
with count(*) as querycount,countrelationships,targetnode
where querycount=countrelationships
return targetnode;

No Rows will be returned if targetnode has more ingoing relationships than connected nodes queried.
Still, if users have DB-accounts, they can use queries as they wish. You'll have to wrap their queries like this in another layer of your software, in your application.
I think what you want to do is not supported by Neo4J.