bseh
bseh

Reputation: 467

Cypher: Find value count inside node property arrays

I'm tring to find all values and their occurrence count inside relationships' property arrays.

I have three nodes and each of them has a relationship property which is a string array.

1- ["444"], 2- ["222", "111"] and 3- ["444"]

What I tried is:

MATCH (:Person { field: "B" })-[r:ALIKE]->(person)
RETURN r.at, COUNT(r.at)

Current result:

r.at            COUNT(r.at)
["222", "111"]  1
["444"]         2

What I expect:

r.at            COUNT(r.at)
["222"]         1
["444"]         2
["111"]         1

or

r.at            COUNT(r.at)
"222"           1
"444"           2
"111"           1

would be better if I get the values instead arrays.

And if one more relationship has "111" and "222" as well, it should be like this:

r.at            COUNT(r.at)
"222"           2
"444"           2
"111"           2

How should I change my current query? Any help would be appreciated.

Upvotes: 0

Views: 1041

Answers (1)

Gabor Szarnyas
Gabor Szarnyas

Reputation: 5057

Let's recreate the test data set:

CREATE
  (:Person {id: 1, at: ['444']}),
  (:Person {id: 2, at: ['222', '111']}),
  (:Person {id: 3, at: ['444']})

To count individual strings, UNWIND each list and count the number of Person nodes (p) for their elements:

MATCH (p:Person)
UNWIND p.at AS at
RETURN at, count(p)

The result is:

╒═════╤══════════╕
│"at" │"count(p)"│
╞═════╪══════════╡
│"111"│1         │
├─────┼──────────┤
│"444"│2         │
├─────┼──────────┤
│"222"│1         │
└─────┴──────────┘

Upvotes: 3

Related Questions