Neo4j cypher return structured map

Let's say we have a database with (: TvShow) - [: contain] → (: season) - [: contain] → (: Episode)

Now let's say everyone wants to query the database for a specific: TvShow and get the result formed like this: {tvshow: // tvShow node //, seasons: [{season: // season node //, episodes: [// episode node //]}]}

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

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

I am trying to work with WITH, collect, FOREACH and the "+" array operator, but haven't had time yet.

Has anyone done this before?

+3


source to share


1 answer


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



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

      

+7


source







All Articles