Jos
Jos

Reputation: 69

Neo4j delete N relation

There are 2 nodes

  1. Posts
  2. User

There is a relationship between 2 nodes called shared

(:User{name:'John'}) -[share:shared]-> (:Posts{name:'1'})

The user has shared news 5 times, so count(share) is 5. Now I need to remove 1 share. i.e. count(share) needs to be changed to 4.

How can this be achieved?

Upvotes: 0

Views: 30

Answers (2)

cybersam
cybersam

Reputation: 66967

I presume that:

  • your DB does not literally contain exactly 2 nodes (only 1 User node and 1 Posts node), and
  • each Posts node only has a single incoming shared relationship

This is how you'd delete one shared relationship for 'John' if you don't care which post gets unshared (and you don't want to delete that post's node):

MATCH (:User {name:'John'})-[share:shared]->(:Posts)
WITH share LIMIT 1
DELETE share;

On the other hand, if you wanted to specifically delete the shared relationship between 'John' and the post with the name of '4' (assuming you don't want to delete that post's node):

MATCH (:User {name:'John'})-[share:shared]->(:Posts {name: '4'})
DELETE share;

Upvotes: 1

logisima
logisima

Reputation: 7458

With something like that :

MATCH (:User{name:'John'}) -[share:shared]-> (:Posts{name:'1'})
WITH share LIMIT 1
DELETE share

Upvotes: 0

Related Questions