Reputation: 934
I have a User
nodes and Intersets
node. I want to be able given an array of interests to create/delete/change the relationship between the User
and the Interests
I also want in the same query to update some properties on the user node.
So far this is what i have menage to do:
MATCH (user:User {id: id})
OPTIONAL MATCH (user)-[oldRel:InterestedIn]->(:Interest)
DETACH DELETE oldRel
WITH user
UNWIND {interestsIds} as id
MATCH (interest:Interest {id: id})
MERGE (user)-[rel: InterestedIn]->(interest)
SET user.name = {user}.name, ..(more sets)
RETURN user, collect(interest) as interests
I think this one is working tho some time is looks like the interests
are returned duplicated..
As well this query looks like a bit of an overkill. Any idea how to do that query with a better way?
Upvotes: 1
Views: 224
Reputation: 10856
Does this seem about right?
MATCH (user:User {id: id})
OPTIONAL MATCH (interest:Interest)
WHERE interest.id IN {interestsIds}
MERGE (user)-[:InterestedIn]->(interest)
WITH DISTINCT user
MATCH (user)-[rel:InterestedIn]->(interest:Interest)
WHERE NOT(interest.id IN {interestsIds})
DELETE rel
WITH DISTINCT user
MATCH (user)-[:InterestedIn]->(interest:Interest)
RETURN user, collect(interest)
Upvotes: 2