Home > database >  How to set outgoing nodes in Cypher?
How to set outgoing nodes in Cypher?

Time:10-22

I have a simple query: given a node A, count the number of nodes that node A has outgoing links, then set that number as a property of A. However I just can't do it.

Attempt 1

MATCH (n)-->(m) 
SET n.out=count(m)
RETURN n.name,n.out

This yields the error:

Invalid use of aggregating function count(...) in this context (line 2, column 11 (offset: 28))
"set n.out=count(m)"
           ^

Attempt 2

MATCH (n)-->(m) 
WITH count(m) AS o 
SET n.out=o 
RETURN n.name,n.out

This yields the error:

Variable `n` not defined (line 3, column 5 (offset: 43))
"SET n.out=o"
     ^

In both times the errors are in the SET clause. But reading the documentation for SET I cannot identify why these happen.

I cannot count the links because for one pair of n, m there maybe several link types.

CodePudding user response:

You can achieve the desired outcome with the following query:

MATCH (n)
WITH size((n)-->()) as out, n
SET n.out = out
RETURN n.name, n.out
  • Related