vvinton
vvinton

Reputation: 121

Return the concatenated string of properties via single Cypher call

I have the next graph:

Theme Contains multiple Stories. Theme and Story are graph nodes, connected via Contains relation.

Each node has property name.

I would like to write a Cypher query so it returns the concatenated string from names.

E.g. Theme.name = "theme name", Story1.name = "s1 name", Story2.name = "s2 name". Query result expected: "theme name s1 name s2 name".

How that can be done?

Upvotes: 6

Views: 3625

Answers (1)

mfkilgore
mfkilgore

Reputation: 126

You have a couple of choices, using either REDUCE or apoc.text.join (from the apoc procedures in 3.x)

something like this -

MATCH (t:Theme)-[:Contains]->(s:Story)
RETURN REDUCE(result=t.name, s in collect(s.name) | result+" "+s)

Or using APOC

MATCH (t:Theme)-[:Contains]->(s:Story)
RETURN apoc.text.join(t.name+collect(s.name)," ")

Upvotes: 10

Related Questions