taher
taher

Reputation: 549

Find a node by its related nodes IDs in neo4j cypher query

I would like to select TotalResult node by related ModelResults nodes with a cypher query.

In the below image I would like to select the specified gray node that is related to exactly 3 green results nodes with IDs [5368, 5410, 5388]

enter image description here I know that the below query is not correct because if you give it IDs[5368, 5410] it will select both gray "total results" nodes.

MATCH (totalResult:TotalResult)-[r:USE]->(modelResult:ModelResult) WHERE ID(modelResult) IN [5368, 5410, 5388] RETURN totalResult, r, modelResult;

I tried to create a query like the below one but it is an invalid query.

MATCH (totalResult:TotalResult)-[r:USE]->(modelResult:ModelResult) 
WHERE ALL(modelResult IN CALL {MATCH (result:ModelResult) WHERE ID(result) IN [5368, 5410, 5388] RETURN result})
AND ALL(CALL{MATCH (result:ModelResult) WHERE ID(result) IN [5368, 5410, 5388] RETURN result} EXISTS nodes(modelResult))
RETURN totalResult, r, modelResult;

Please let me know is it possible to select a node by its exactly related nodes IDs and how?

Thanks.

Upvotes: 0

Views: 270

Answers (1)

cybersam
cybersam

Reputation: 66999

Assuming the list of desired ModelResult ids is passed as an ids parameter, this should return a result if the totalResult is related to all of the specified ModelResults and no others:

MATCH (totalResult:TotalResult)-[r:USE]->(modelResult:ModelResult)
WITH totalResult, COLLECT(ID(modelResult)) AS mIds, COLLECT({r:r, modelResult:modelResult}) AS data
WHERE SIZE(data) = SIZE($ids) AND ALL(id IN $ids WHERE id IN mIds)
RETURN totalResult, data;

Upvotes: 1

Related Questions