Spring Data Neo4j return count of affected nodes after update operation

60 views Asked by At

Is there any way to return the count of affected nodes after doing the update operation with Spring Data Neo4j repository?

e.g

@Query("MATCH (n:Student) WHERE n.id IN $idList SET n.isRegistered = true")
Integer updateRegisterInfo(@Param("idList") List<String> idList);

But sdn can only return void/null value for such operation.

Thanks

1

There are 1 answers

3
Finbar Good On

If you want the count of nodes to which the SET was applied, return the count of rows:

MATCH (n:Student) WHERE n.id IN $idList 
SET n.isRegistered = true
RETURN count(*) AS numAffected

If you want the count of nodes whose value of isRegistered was changed, remove nodes that already have isRegistered = true with the WHERE clause:

MATCH (n:Student) 
WHERE n.id IN $idList AND NOT coalesce(n.isRegistered, false) 
SET n.isRegistered = true
RETURN count(*) AS numAffected

(I've just formatted as stand-alone Cypher to make it easier read.)