0
votes

I would like to create a relationship for any 'tag' in Feature node array property 'aTags' which equals a Tag node. I've tried using FOREACH and UNWIND without any luck.

Data:

  1. (a) are Feature nodes with a property array of tags.
    • a.aTags is an array similar to {1,2,3,4,5}
  2. (b) are Tag nodes
    • b.tag matches a single element in a.aTags

Cypher:

START a=node(*), b=node(*)
FOREACH (a_tag IN a.aTags = b.tag |
    CREATE (a)-[:HAS_TAG]->(b) );

Thanks in advance for any insight!

1

1 Answers

1
votes

How about

match (a:Feature)
FOREACH (aTag in a.aTags | merge (b:Tag {name:aTag}) merge (a)-[:HAS_TAG]->(b));

Note however, that this will create a b:Tag if there is a value in aTags for which a Tag node does not already exist.

I created some data here http://console.neo4j.org/r/xc8d0 using

create (_0:`Tag` {`name`:"tagA"})
create (_1:`Tag` {`name`:"tagB"})
create (_2:`Tag` {`name`:"tagC"})
create (_3:`Tag` {`name`:"tagD"})
create (_4:`Feature` {`aTags`:["tagA"]})
create (_5:`Feature` {`aTags`:["tagB", "tagC"]})
create (_6:`Feature` {`aTags`:["tagA", "tagC"]})

Let me know if that's not what you meant.