Lee
Lee

Reputation: 791

neo4j cypher joining 2 nodes merge

I have 2 node tags: User, Tag.

Lets say that I have a user node that exists.

Is it possible to match that node, and then if the tag exists merge between them, and if the tag doesn't exist create the tag.

I tryed:

MATCH (n:User {name: "user"}) MERGE (n)-[r:follow]->(tag:Tag {name: "notexist")

In the above example it creates the node "notexist" and the relationship. But if I have a node that is named "notexist" it doesn't merge, instead it creates another tag named "notexist"

thank you

Upvotes: 1

Views: 805

Answers (1)

Jim Biard
Jim Biard

Reputation: 2272

Lee,

Here's how to do this.

MATCH(n:User {name: 'user'})
WITH n
MERGE (t:Tag {name: 'notexist'})
WITH n, t
MERGE (n)-[r:follow]->(t);

Grace and peace,

Jim

Upvotes: 4

Related Questions