Richard.Davenport
Richard.Davenport

Reputation: 1501

Cypher results return certain structure

I'm trying to return a certain structure. Here is my query:

MATCH (tracker:tracker { active: true }) OPTIONAL MATCH (tracker { active: true })--(timer:timer) RETURN { tracker:tracker, timers:COLLECT(timer) } as trackers

Here is what I am returning so far:

{
  "results": [{
    "columns": ["trackers"],
    "data": [{
      "row": [{
        "tracker": {
          "title": "a",
          "id": "04e3fddc-5aef-4c3a-9aeb-62a9fb15bd75",
          "active": true
        },
        "timers": []
      }]
    }]
  }],
  "errors": []
}

I would like the timers to be nested under the "tracker" with the tracker's properties, like this:

{
  "results": [{
    "columns": ["trackers"],
    "data": [{
      "row": [{
        "tracker": {
          "title": "a",
          "id": "04e3fddc-5aef-4c3a-9aeb-62a9fb15bd75",
          "active": true,
          "timers": []
      }]
    }]
  }],
  "errors": []
}

Upvotes: 0

Views: 46

Answers (1)

William Lyon
William Lyon

Reputation: 8546

Try this:

MATCH (tr:tracker {active: true}) 
OPTIONAL MATCH (tr)--(ti:timer) 
WITH { 
    title: tr.title, 
    id: tr.id,
    active: tr.active,
    timers: COLLECT(ti)
} as trackers
RETURN trackers

Upvotes: 1

Related Questions