cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

How to limit query results by count of node type

gq16
Node Clone

Hey all!

I'm running the following query, but getting errors, and I'm not sure why since it conceptually makes sense to me:

MATCH (p:Person)-[:HAS_CONTACT_RECORD]->(c:Contact)-[:HAS_EMAIL]->(c2:PrimaryEmail)
WHERE count(c)>2
RETURN *

gq16_1-1658344948830.png

 

this is my schema,

gq16_2-1658345024865.png

and this is what the query returns when I exclude the "where clause". What I want is to be able to see only the people and PrimaryEmails related to more than 2 Contacts (the green nodes).  So in the image below, assuming the query worked as expected, I would have only seen the piece in the upper center with the name "Steve Zoelick"

gq16_0-1658344696824.png

Let me know what the proper and most efficient way to do this is!

 

Thanks guys!

GQ

 

 

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

Hello @gq16 😊

You can't directly use an aggregating function in the WHERE clause.

MATCH (p:Person)-[:HAS_CONTACT_RECORD]->(c:Contact)-[:HAS_MAIL]->(e:PrimaryEmail)
WITH p, collect(c) AS contacts, collect(e) AS emails
WHERE size(contacts) > 2
RETURN *

Regards,
Cobra

View solution in original post

1 REPLY 1

Cobra
Ninja
Ninja

Hello @gq16 😊

You can't directly use an aggregating function in the WHERE clause.

MATCH (p:Person)-[:HAS_CONTACT_RECORD]->(c:Contact)-[:HAS_MAIL]->(e:PrimaryEmail)
WITH p, collect(c) AS contacts, collect(e) AS emails
WHERE size(contacts) > 2
RETURN *

Regards,
Cobra