Pat
Pat

Reputation: 2238

How do I collect a list of properties from related nodes in Cypher?

I have a bunch of Foo objects in my graph, and they may have 0 to n related Bar objects. Bar objects have a property I'm looking to aggregate against, baz. Here's what I've started with:

MATCH (f:Foo)--(b:Bar) RETURN f, COLLECT(b.baz)

I'm trying to get output that looks like the following:

===============================
| f         | COLLECT(b.baz)  |
-------------------------------
| Node(1..) | ["1", "2", "3"] |
| Node(2..) | ["4", "5"]      |
===============================

Instead, I'm getting what looks like this:

===============================
| f         | COLLECT(b.baz)  |
-------------------------------
| Node(1..) | ["1"]           |
| Node(1..) | ["2"]           |
| etc...                      |
===============================

Any insight as to what I'm doing wrong?

Upvotes: 0

Views: 890

Answers (1)

Gabor Szarnyas
Gabor Szarnyas

Reputation: 5047

Can you create an example dataset? For this dataset your query works fine.

CREATE
  (n1:Foo {name: '1'}),
  (a:Bar {name: 'a', baz: 1}),
  (b:Bar {name: 'b', baz: 2}),
  (n1)-[:REL]->(a),
  (n1)-[:REL]->(b)

Your query returns the following:

MATCH (f:Foo)--(b:Bar)
RETURN f, COLLECT(b.baz)

╒═════════╤══════════════╕
│f        │COLLECT(b.baz)│
╞═════════╪══════════════╡
│{name: 1}│[1, 2]        │
└─────────┴──────────────┘

Upvotes: 2

Related Questions