Search code examples
neo4jcypher

return nodes type i having at least n relationship to n other nodes type s


I have 2 type of nodes: Investor and Startup

I want to create a query whereas users can give, say, 3 as input, and only Investors having relationship with at least 3 different startups would be return. This solution will be used in neo4j Bloom

My attempt:

    MATCH p = (s1:Startup)<-[r:INVESTOR_INVESTED_IN]-(i:Investor)-[r2:INVESTOR_INVESTED_IN]->(s2:Startup)
    WHERE id(s1) in $nodes AND id(s2) in $nodes AND s1<>s2
    RETURN p

With my attempt above, it is not obivous how to change from minimum 2 to say, minimum 4


Solution

  • Find all investors who invested to a startup and check the number of startups in the collection. If you want to return the path then you need to return the path like below:

    MATCH (i:Investor)-[:INVESTOR_INVESTED_IN]-> (s:Startup)
    WITH i, collect(distinct s) as startup WHERE size(startup) >= 3
    MATCH p = (i)-[:INVESTOR_INVESTED_IN]-> (:Startup) 
    RETURN p 
    

    But if you are only interested to display the investors, query will be simpler like below:

    MATCH (i:Investor)-[:INVESTOR_INVESTED_IN]-> (s:Startup)
    WITH i, collect(distinct s) as startup WHERE size(startup) >= 3
    RETURN i