Reputation: 467
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?
Upvotes: 4
Views: 3073
Reputation: 51
By following William Lyon's answer, I was able to implement a batch update of multiple properties as follows:
UNWIND [{id: 123456, name: "John", age: 27}, {id: 789012, name: "Jane", age: 24}] AS updateUser
MATCH (user: User)
WHERE user.id = updateUser.id
SET user += updateUser
RETURN user
Upvotes: 2
Reputation: 8546
Assuming you have pairs of userId
s 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
Upvotes: 5