Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j cypher return structured map

Lets say, we have a database with (:TvShow)-[:contain]->(:Season)-[:contain]->(:Episode)

Now lets say, one wants to query the database for a specific :TvShow and get a result shaped this way : {tvshow : //The tvShow node// , seasons: [ {season: //Season node// , episodes: [//episode node//]}]}

For exemple: if we have OneShow with 2 seasons and 3episodes

the result would be a json object: {tvshow: OneShow, seasons: [{season: Season1, [episode1-1,episode1-2,episode1-3]},{season: Season1, [episode2-1,episode2-2,episode2-3]}]}

I'm trying to work with WITH, collect, FOREACH and the array operator '+' but without success yet.

Anyone has done this before?

like image 800
Hugo Avatar asked Sep 13 '25 22:09

Hugo


1 Answers

Based on the following Neo4j console dataset http://console.neo4j.org/r/7uru0d, you can achieve it in this way :

MATCH (n:TvShow)-[:HAS_SEASON]->(season)-[:HAS_EPISODE]->(episode)
WITH n, season, collect(episode) AS ep
RETURN { show: n.id, seasons:collect({ season: season.name, episodes: ep })} AS shows
like image 51
Christophe Willemsen Avatar answered Sep 15 '25 15:09

Christophe Willemsen