Get child nodes from main node

Im trying to get all the nodes that belong to ukcoastal in this image, so navarea 1 has messages that belong to it, uk coastal also has messages that belong to it, how do I get this back in the query im using

MATCH (message:NavareaMessage{active:true})-[r:BelongsTo]->(navarea:Navarea{id:1}) OPTIONAL MATCH (coastalzones)-[c:IsCoastalOf]->(navarea)
return message,navarea, coastalzones

thanks

It's hard to tell what your true inputs and outputs are supposed to be (this looks like just a hardcoded example). Which nodes are your real starting nodes, and which nodes are unknown (besides the connected messages)?

Also, do you want any grouping in your returned data? You can use collect() to aggregate nodes.

An example of desired output would help us understand what you want.

I have hardcoded it to navarea 1 that has id 1. im pretty much looking for this in the image

I have 22 navareas which may or may not include a costal component, the image above was the original query but with me opening the nodes in the neo4j client

So is your interest the graphical results view only in the browser, or are you expecting some desired textual output format?

for now just graphical, im only modelling it all out now

Oh, in that case this should work:

MATCH path = (:NavareaMessage{active:true})-[:BelongsTo]->()-[:IsCoastalOf*0..1]->(:Navarea{id:1}) 
RETURN path

We have our starting node, and we're interested in the active messages that :BelongsTo both the starting node and potentially other nodes that could be connected by an :IsCoastalOf relationship (the *0..1 treats this as an optional expansion).

ok thanks, that works really well visually, and when I expand it to not just navarea 1 I can see how its all laid out. Ill have a toy around and see what I can do from it, ill start thinking about how the data is returned and how I can format it so I can consume the data better from my external app. one more point, ive set the data out so it goes from bottom up eg message belongs to navarea, is there any difference from doing navarea contains message?

There's no difference performance-wise, just whatever makes the most sense to you.

yeah I was aware performance wise its the same, but how about the format of the data coming back? I can see from the results table every row has a navarea and its details, so that's lots of duplicate data coming back, which is fine for the visual, if for every row in the results I get a Navarea, properties eg list of messages and a list of coastalzone that has a property that's a list of messages etc, is it purely the query that affects that or is how I lay out the data important?