Reputation: 467
Using CYPHER I can get an ordered list of things using the collect() function. Is it possible to convert such a list into a simple string so that it behaves as a single string object?
If this isn't possible is it possible to somehow concatenate sequentially the contents of two (or more) collect statements so that in a single row I can produce output such as 'A,B,C a,b,c' where A,B,C is the ordered product of the 1st collect statement and a,b, c the second?
Upvotes: 5
Views: 3623
Reputation: 66967
A more general solution that will also work for collections of objects (not just collections of strings) is to convert the concatenated collections into stringified JSON using the APOC procedure apoc.convert.toJson.
This example using string collections:
WITH ['A','B','C'] AS x, ['a','b','c'] AS y, ['do','re','mi'] AS z
WITH x + y + z AS data
CALL apoc.convert.toJson(data) YIELD value
RETURN value
will return this:
"[\"A\",\"B\",\"C\",\"a\",\"b\",\"c\",\"do\",\"re\",\"mi\"]"
This example using object collections:
WITH [{a:1},{b:2},{c:3}] AS x, [{a:10},{b:20},{c:30}] AS y, [{a:100},{b:200},{c:300}] AS z
WITH x + y + z AS data
CALL apoc.convert.toJson(data) YIELD value
RETURN value
will return this:
"[{\"a\":1},{\"b\":2},{\"c\":3},{\"a\":10},{\"b\":20},{\"c\":30},{\"a\":100},{\"b\":200},{\"c\":300}]"
Upvotes: 0
Reputation: 2507
To flesh out Dave's comment: First, you'll want to combine your collections, then use REDUCE()
to append each item to a string. Like so:
WITH COLLECT(first_group) AS a, COLLECT(second_group) AS b
WITH a + b AS c
WITH REDUCE(s = HEAD(c), n IN TAIL(c) | s + ', ' + n) AS result
RETURN result
Check out the docs on REDUCE
to get a better idea how it works in Cypher.
Upvotes: 4