Neo4j, get all relationships between a set of nodes

后端 未结 6 1122
遥遥无期
遥遥无期 2021-01-05 00:13

I\'ve a query that fetches nodes based on a property

MATCH (c { type: \'sometype\' })
WITH c LIMIT 100
RETURN c

all I want is to also fetch

6条回答
  •  轮回少年
    2021-01-05 00:56

    I guess there are multiple ways to do that. One approach is to find all nodes for the given type and build a collection out of them (cs). Then match again that group, this time with outgoing relationships to any node and filter that to make sure the endnode is in cs:

    MATCH (c {type:'sometype'})
    WITH collect(c) as cs
    MATCH (x {type:'sometype'})-[r]->(y)
    WHERE y in cs
    RETURN startNode(r).name, endNode(r).name
    

    Don't know your graph model, but I think it could be a good idea to refactor the property type='sometype' into a label sometype. In this case the query would look like this:

    MATCH (c:Group1)   
    WITH collect(c) as cs
    MATCH (x:Group1)-[r]->(y)
    WHERE y in cs
    RETURN startNode(r).name, endNode(r).name
    

提交回复
热议问题