Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Batch update of data

Tags:

neo4j

How can I do multiple Nodes update in neo4j cypher?

Now I'm trying to do like this:

MATCH (user151325288158:User{userId:151325288158}),
      (user88245:User{userId:88245}) 
SET user151325288158.balance=2902833.4219789803 
SET user88245.balance=146701.0299999991 
RETURN user151325288158.balance,user88245.balance;

But here I have problem that if such user is absent in DB, nobody will be updated. Other problem is performance, such queries are slow.

Is there some method to do such bulk updates?

like image 984
SoulCub Avatar asked Sep 17 '25 10:09

SoulCub


1 Answers

Assuming you have pairs of userIds and new balance values in an array of maps/dicts, like this:

[
    {
      "userId": 151325288158,
      "balance": 146701.09
    },
    {
      "userId": 887436512344,
      "balance": 22453.34
    },
    {
      "userId": 873927654232,
      "balance": 300002.22
    }
]

You can pass this array as a parameter to a Cypher query to MATCH on the userId and update the balance property like this:

WITH {data} AS pairs
UNWIND pairs AS p
MATCH (u:User) WHERE u.userId = p.userId
SET u.balance = p.balance 
like image 157
William Lyon Avatar answered Sep 19 '25 08:09

William Lyon