Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-03-2019 04:32 PM
Hi guys.
I have node user and node post. So user can either like or dislike that post. When he likes post i create LIKED relationship between user and post, but if user then dislike it i will create DISLIKE relationship but LIKED still remains. How can i create one relationship and delete other one?
This is my query:
MATCH (user:User), (post:Post)
WHERE user.username = '...' and post.id = '...'
MEREGE (user)-[liked:LIKED]->(post)
ON CREATE liked
SET liked.time = '...'
Thank you in front guys 🙂
01-03-2019 09:30 PM
With apoc.refactor.setType() you can change the relationship type:
// LIKED............................
CREATE (c:User {name:"a"}), (d:Post {id:1})
CREATE (c)-[:LIKED]->(d)
RETURN c, d;
//Change to DISLIKED....................
MATCH (c:User {name:"a"})-[r:LIKED]->(d:Post {id:1})
CALL apoc.refactor.setType(r, 'DISLIKED') YIELD input, output RETURN c, d;
-Kamal
01-04-2019 04:04 AM
But what if liked relationship doesnt exists? Then this second query is useless, am i right?
01-04-2019 09:05 AM
The query is to change the existing relationship type.
01-05-2019 04:22 PM
:param IsLiked: true
MATCH (user:User), (post:Post)
WHERE user.username = '...' and post.id = '...'
//expression when LIKED will create relationship otherwise command will skipped
FOREACH(_ in case when IsLiked then [1] else end | MERGE (user)-[:LIKED]->(post)
//expression when DISLIKED will create relationship otherwise command will skipped
FOREACH(_ in case when not IsLiked then [1] else end | MERGE (user)-[:DISLIKED]->(post)
01-07-2019 04:09 PM
probably also add an optional match for any relationship
and delete it if it's of the wrong type
in general an unconditional merge and an apoc.setType might be easiest.
All the sessions of the conference are now available online