cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to avoid duplicate nodes

The code written below should check if a match is present, if so it should update the value.
Otherwise it should create a node with the given value.

Currently every time I update a organization.Role value it creates an entirely new node.
Even when the organization.Role value is not changed.
So technically 60 seller nodes could be added.
There should only be one role node per organization and its value should just be changed.
I've not been able to figure out why the current behavior happens as stated above.

query = query.Merge("(role:Role {roletype: $newRoleValue})")
                        .OnCreate()
                        .Set("role = $newRole")
                        .WithParam("newRole", new { Name = newTenderVersion.Organization.Role.ToString() })
                        .WithParam("newRoleValue", newTenderVersion.Organization.Role.ToString())
                        .Merge($"(organization)-[:HAS_ROLE]->(role)");
2 REPLIES 2

What kind of DSL is that?
What does the generated query look like?

Were does organization come from?
You need to match or merge it first, otherwise it will be created by MERGE.

clem
Graph Steward

I'm having trouble reading this as I'm used to Cypher. I'm confused to what you want to do exactly.

When you say

it creates an entirely new node
Do you mean a new Organization node?

But, I believe what you need to do, is have MATCH statements to get the existing Organization nodes you don't want to duplicate.

Then do a MERGE on the the relationships and/or the role Nodes that are new.

Maybe something like:

MATCH (org:Organization) WHERE ...
MERGE(newRole:Role) WHERE ...
MERGE (org)-[:HAS_ROLE]->(newRole)
...

I hope that helps.