Reputation: 8776
I have to update some properties on en entity (we extracted some user properties to a separate entity). Now I'm trying to write a query that updates the existing data. This is what I have so far:
// Match the user and it's settings
MATCH (s:UserSettings)-[:SETTINGS_FROM]->(u:User)
// Set the properties of the settings node to their old value from user entity
SET s.`settings.key1` = u.key1
SET s.`settings.key2` = u.key2
SET s.`settings.key3` = u.key3
// Remove the keys on the user entity
SET u.key1 = null
SET u.key2 = null
SET u.key3 = null
RETURN s, u;
This works for users that already have a UserSettings
node attached, but not for users without.
I tried to replace MATCH
with MERGE
- this will create a new user although all users are available in the db. What am I doing wrong?
Upvotes: 0
Views: 684
Reputation: 8970
Using MERGE
on a path means that if any of the path elements is missing, the whole pattern will be created.
Usually, you want to MERGE
specific nodes and relationships, not a whole path at once.
Assuming:
SETTINGS_FROM
relationship (otherwise, you'd have to MERGE
first on the settings node pattern AND then on the relationship)You should run something like:
MATCH (u:User)
MERGE (s:UserSettings)-[:SETTINGS_FROM]->(u)
ON CREATE
SET s.`settings.key1` = u.key1
SET s.`settings.key2` = u.key2
SET s.`settings.key3` = u.key3
REMOVE u.key1 // those removes are not part of ON CREATE (see https://github.com/neo4j/neo4j/issues/6172)
REMOVE u.key2
REMOVE u.key3
Upvotes: 1