iR0Nic
iR0Nic

Reputation: 340

Neo4j relate nodes by same property

I have a Neo4J DB up and running with currently 2 Labels: Company and Person.

Each Company Node has a Property called old_id.
Each Person Node has a Property called company.

Now I want to establish a relation between each Company and each Person where old_id and company share the same value.

Already tried suggestions from: Find Nodes with the same properties in Neo4J and Find Nodes with the same properties in Neo4J

following the first link I tried:

MATCH (p:Person) 
MATCH (c:Company) WHERE p.company = c.old_id 
CREATE (p)-[:BELONGS_TO]->(c)

resulting in no change at all and as suggested by the second link I tried:

START 
    p=node(*), c=node(*) 
WHERE 
    HAS(p.company) AND HAS(c.old_id) AND p.company = c.old_id
CREATE (p)-[:BELONGS_TO]->(c)
RETURN p, c;

resulting in a runtime >36 hours. Now I had to abort the command without knowing if it would eventually have worked. Therefor I'd like to ask if its theoretically correct and I'm just impatient (the dataset is quite big tbh). Or if theres a more efficient way in doing it.

Upvotes: 0

Views: 1423

Answers (2)

Michael Hunger
Michael Hunger

Reputation: 41706

Depending on the size of your dataset you want to page it

create constraint on (c:Company) assert c.old_id is unique;

MATCH (p:Person) 
WITH p SKIP 100000 LIMIT 100000
MATCH (c:Company) WHERE p.company = c.old_id 
CREATE (p)-[:BELONGS_TO]->(c)
RETURN count(*);

Just increase the skip value from zero to your total number of people in 100k steps.

Upvotes: 0

cybersam
cybersam

Reputation: 67044

This simple console shows that your original query works as expected, assuming:

  1. Your stated data model is correct

  2. Your data actually has Person and Company nodes with matching company and old_id values, respectively.

    Note that, in order to match, the values must be of the same type (e.g., both are strings, or both are integers, etc.).

So, check that #1 and #2 are true.

Upvotes: 1

Related Questions