1

I'm trying out Structr, which seems great, but I'm running into an issue where I want to loop through a returned collection and output an html element for each item in it. It's a little complicated by the fact that I also need to use merge_unique on the collection before I do so, because the old version of neo4j Structr uses chokes on a combination of optional match and collect(distinct n).

Here's the request I'm making in the cypher query pane of Structr:

MATCH (c:Country) WHERE c.code = 'US'
Optional match (c)<-[:THING_IN]-(t:Thing)-[:THING_BELONGS_TO]-(p:Project)
optional match (p)<-[:SPONSORS]-(sp)
where p.status = "ACTIVE"
with c, collect(distinct p) as projects, collect(sp.name) as sponsors
RETURN {
    name:c.name, 
    code:c.code, 
    open_projects:size(filter(x IN projects WHERE x.status = "ACTIVE")),
    closed_projects:size(filter(x IN projects WHERE x.status <> "ACTIVE")),
    all_projects:size(projects),
    sponsors:sponsors
}

So, "sponsors" is returned as a non-distinct collection of all the names of the sponsors of the projects. It works to then loop through the sponsors if they're returned as objects, and display ${sponsor.name} (after creating a data-binding with sponsor = merge_unique(data.sponsors), but I can't just display each sponsor from a collection of string variables.

I'm not sure I'm explaining this very well, but if you've used Structr, I expect this is a problem you've run into. If you can point me in the right direction, thanks in advance.

Daniel
  • 77
  • 7

1 Answers1

2

Reproducing your query with the following test data, I get the same result (non-distinct list of sponsor names).

Data

If I add a distinct to the Cypher statement, it returns a distinct collection of the sponsors:

[...]
with c, collect(distinct p) as projects, collect(distinct(sp.name)) as sponsors
[...]

Note that the collection in sponsors is already a String array so you can't use a Structr repeater easily (there's a way but it's more complex).

To use Structr's data binding directly, modify the query to return objects (sp.name => sp) and create a nested repeater with a function query (in my example bound to the <b> element:

[...]
with c, collect(distinct p) as projects, collect(distinct(sp)) as sponsors
[...]

enter image description here

Axel Morgner
  • 2,292
  • 16
  • 15