Cypher - get all associated relationships for a node

Viewed 54

I have the following setup:

A doctor can have multiple patients, and every patient can have multiple diagnosis. I need to return for Doctor X, all his associated patients and their associated diagnosis. How can I write a nested relationship query as follows? I have the following query for now:

 MATCH
(p: Patient)-[:PATIENT_AT]->(d:Doctor),
(di:Diagnosis)-[:DIAGNOSIS_BY]->(d:Doctor),
(p:Patient)-[:HAS_DIAGNOSIS]->(di:Diagnosis)
WHERE
 d.id=1000000
 RETURN p,di

But this is returning me two separate queries. Can I get the same result in one query like:

   patient 1--> all diagnosis
   patient 2 --> all diagnosis

Or do I need two separate loops?

1 Answers

For a single query: probably something like this would do what you're looking for (untested, since I don't have your data, but... should be close):

MATCH (doc:Doctor {id=1000000})<-[:PATIENT_AT]-(pat:Patient)-[:HAS_DIAGNOSIS]->(diag:Diagnosis)
return doc,pat,collect(diag)

This particular query ignores the direct relationship between (:Doctor) and (:Diagnosis) but I don't think it's needed for the results you're looking for, since each patient is associated with the diagnoses.

Related