jwebb
jwebb

Reputation: 1149

Neo4j, Cypher Use COUNT with COLLECT to return JSON

I'm trying to return a JSON object like this:

{   "rels": [
    {
      "rel": "type_1",
      "foos": 15
    },
    {
      "rel": "type_2",
      "foos": 2073
    },
    {
      "rel": "type_3",
      "foos": 23
    }   ] }

Using the query.

MATCH (n:bar)<-[r:REL]-(n1:foo)
WHERE n.bar_id='1234567890'
RETURN collect({rel: r.rel_status, foos: count(r.rel_status)}) AS rels

Where the REL relationships have a property called rel_status.

ERROR: Can't use aggregate functions inside of aggregate functions.

Upvotes: 0

Views: 1816

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30407

This is because you're attempting to use count() inside a collect(), which isn't allowed. You'll need to do these one step at a time.

MATCH (n:bar)<-[r:REL]-(:foo)
WHERE n.bar_id='1234567890'
WITH r.rel_status as rel, count(r) as foos
RETURN collect({rel: rel, foos: foos}) AS rels

Upvotes: 2

Related Questions