3
votes

Is there any way to do a cypher query that returns only the outgoing relationship with cardinality of 1, in neo4j 1.9.7?

e.g

N2 ----> N4 -----> N10
|        |-------> N9
|
|------> N5 -----> N9
|
|------> N6 -----> N9  

In a structure like this i would like to traverse the nodes and return just the nodes that have only one outgoing relationship (so N5 and N6 in the example).

I can do it with the Java API using the IteratorUtil class to get the count

Node process = db.getNodeById(2);

        for(Relationship rel : process.getRelationships(Direction.OUTGOING))
        {
            Node appProcess = rel.getOtherNode(process);
            if(IteratorUtil.count(appProcess.getRelationships(Direction.OUTGOING).iterator()) == 1)
            {
                System.out.println(appProcess.getId()+" is a vital process");
                count++;
            }
        }

I would like to do the same in Cypher.

1

1 Answers

3
votes

Not sure if this will work, and I don't have 1.9 running at the moment, but it has worked like this in the past.

START n=node(2)
MATCH (n)-->(m)
WHERE length((m)-->()) = 1
RETURN m