Home  >  Article  >  Spring Data Neo4j returns count of affected nodes after update operation

Spring Data Neo4j returns count of affected nodes after update operation

PHPz
PHPzforward
2024-02-22 14:01:191153browse

php editor Xigua brings the latest java Q&A: How does Spring Data Neo4j return the count of affected nodes after an update operation? Spring Data Neo4j is a powerful graph database framework that provides rich functions and flexible API. When performing an update operation, sometimes it is necessary to obtain the number of affected nodes after the update for subsequent processing. This article will introduce how to use Spring Data Neo4j to implement this function and help developers make better use of the features of graph databases.

Question content

After performing an update operation using the spring data neo4j repository, is there any way to return the number of affected nodes?

For example

@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 values ​​for such operations.

Thank you

Workaround

If you want the number of nodes to which set is applied, return the number of rows:

match (n:student) where n.id in $idlist 
set n.isregistered = true
return count(*) as numaffected

If you want a count of nodes with isregistered values ​​that have changed, use the where clause to delete nodes that already have isregistered = true:

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

(I just formatted this as a standalone cypher to make it easier to read.)

The above is the detailed content of Spring Data Neo4j returns count of affected nodes after update operation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete