Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-16-2022 11:32 PM
Hi,
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.
05-17-2022 01:11 AM
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!
05-17-2022 02:01 AM
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.
05-17-2022 02:16 AM
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.
05-17-2022 03:37 AM
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.
05-17-2022 05:15 AM
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.
All the sessions of the conference are now available online