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.

Creating nodes from a distinct query

We have run into a situation where we need to create a master node for certain templates.

We can get the distinct templates easy enough:

match (b {KDM:'TemplateParameter'}) return distinct(b.Label)

But I am not clear on how I would create 1 node from each distinct(b.Label). Tried this one

match (b {KDM:'TemplateParameter'}) distinct(b.Label) as Templates
with Templates merge (a:LibNode {Label:Templates.Label})

We need some of the properties from the b nodes in addition to the label but that is easy to do later
Thanks

1 ACCEPTED SOLUTION

ameyasoft
Graph Maven
Try this:

with collect (distinct b.Label) as Templates
FOREACH (plates in Templates |
	merge (a:LibNode {Label:plates})
)

View solution in original post

4 REPLIES 4

Well we already had the distinct. So the first query in my question returns a set of unique templates. The query you gave was really no improvement on that UNLESS

propertyvalues can somehow be used directly in the create ? Can't use APOC here.

ameyasoft
Graph Maven
Try this:

with collect (distinct b.Label) as Templates
FOREACH (plates in Templates |
	merge (a:LibNode {Label:plates})
)

That was nicely done. Loved the alliteration on "plates". Thank you

Thanks for your appreciation!