Science
Science

Reputation: 147

Update more than one nodes with the same label at the same time in neo4j

How can i update these three nodes with the same label at the same time in neo4j.I tried the following but it will not work.

Method 1:

START n=node(0) 
MATCH n-[r:HAS_GRADE]->(g:Grade)-[r1:HAS_SYLLABUS]->(sy:Syllabus)
OPTIONAL MATCH (sy)-[:HAS_CHILD]->(s:Subject) SET s:Publish WITH s
OPTIONAL MATCH (s)-[:HAS_CHILD]->(c:Chapter) SET c:Publish WITH c
OPTIONAL MATCH (c)-[:HAS_CHILD]->(t:Topic) SET t:Publish 
RETURN s;

Method 2

    START n=node(0) 
    MATCH n-[r:HAS_GRADE]->(g:Grade)-[r1:HAS_SYLLABUS]->(sy:Syllabus)
    OPTIONAL MATCH (sy)-[:HAS_CHILD]->(s:Subject)
    OPTIONAL MATCH (s)-[:HAS_CHILD]->(c:Chapter)
    OPTIONAL MATCH (c)-[:HAS_CHILD]->(t:Topic) WITH s,c,t
    SET s:Publish  
    SET c:Publish
    SET t:Publish
    RETURN s;

Method 3

START n=node(0) 
MATCH n-[r:HAS_GRADE]->(g:Grade)-[r1:HAS_SYLLABUS]->(sy:Syllabus)
OPTIONAL MATCH (sy)-[:HAS_CHILD]->(s:Subject)
OPTIONAL MATCH (s)-[:HAS_CHILD]->(c:Chapter)
OPTIONAL MATCH (c)-[:HAS_CHILD]->(t:Topic) WITH s,c,t
SET s:Publish, c:Publish, t:Publish
RETURN s

Do i need to run this query 3 times to update the three nodes. Or is there any alternate methods? Please help.

Upvotes: 1

Views: 73

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39925

How about:

START n=node(0) 
MATCH n-[r:HAS_GRADE]->(g:Grade)-[r1:HAS_SYLLABUS]->(sy:Syllabus)-[:HAS_CHILD*]->(c)
WHERE any(x in labels(c) WHERE x in ["Subject","Chapter","Topic"])
SET c :Publish
RETURN c

The WHERE makes sure that the end nodes of the HAS_CHILD carries one the labels given in the list.

We're using a variable path length (see the *) with HAS_CHILD to grab all the children nodes.

Upvotes: 2

Related Questions