Neo4j query for relationship

Hello guys, i have existing five node with same label and i want to create one other node and also create a relation with all existing five node i was try but its create one to one relation on one to many relation. please help.
thanks

match (e:video) create(v:videomaster{name:'videomaster'}),
(e)-[r:DO_SHOPPING_WITH ]->(v)

see screen sort

But i want to like this at creation time of node

MATCH p = (u:user)
merge (v:u_master{name:"usermaster"})
FOREACH (n IN nodes(p)| merge(n)-[r:test]->(v))

1 Like

While that works, that's more complicated than it needs to be. You can shorten this to:

MATCH (u:user)
MERGE (v:u_master{name:"usermaster"})
MERGE (n)-[r:test]->(v)

The idea is that after the match in the first line, you have a number of rows of results, one per u node. Then for each of those rows, MERGE executes to create the v node (only the first of those merges creates the node, the others match to that just-created node). The last line then executes and the relationship is created between each u node and the v node.

That said, it would be a bit more efficient to reorder the query instead:

MERGE (v:u_master{name:"usermaster"})
WITH v
MATCH (u:user)
MERGE (n)-[r:test]->(v)

With this, the single v node is created (or matched to if it already existed). Then we match to the u nodes (each one will have the same v on its row). Then the relationships are merged between them.

1 Like

Can somebody please explain me what this "u_master" is?
I am having the same problem with my merge where I have a table in the strucuture

Name Role Project
Paul Owner Project1
Paul Owner Project2
Paul Owner Project3

I currently have the query:

MATCH (p:Person {name: "Paul"})-->(n:Project)
MERGE(p)-[:HAS_ROLE]->(n)
RETURN p,n

In the previous example, the :u_master node was a singleton node with the label of :u_master and the property of name:"usernamster". The idea was to only create a single one of these nodes and connect that single node to all other :user nodes.

For your query, the MATCH pattern suggests that Paul already has a relationship of some type to a :Project node (the match will fail if such a pattern doesn't exist, shortcircuiting the query), then your MERGE merges in a :HAS_ROLE relationship to that node (creating it if it doesn't already exist).

In order to help, we'll need to know what exactly you're trying to do here, and what the query is doing that isn't what you're expecting.