Skip to content

Instantly share code, notes, and snippets.

@mneedham
Forked from jexp/create_users.adoc
Last active August 29, 2015 14:01
Show Gist options
  • Save mneedham/ad23156a717f2965430d to your computer and use it in GitHub Desktop.
Save mneedham/ad23156a717f2965430d to your computer and use it in GitHub Desktop.

How to create relatonships between elements in a collection

This GraphGist answers a Stackoverflow question.

Creating A Sample User Graph

We want to create a social network, and have to connect some people. Let’s do that in two steps.

Create a few users

UNWIND ["Amanda","Michael", "Max","Magnus","Mark","Peter","Andres"] AS name
CREATE (:User {name:name})

Connecting users starting with M

MATCH (u:User) WHERE u.name =~ 'M.*'
WITH collect(u) as users
WITH users[..-1] AS c1, users[1..] as c2
UNWIND reduce(acc=[], x in range(0,length(c1) -1) | acc+ [ [c1[x], c2[x]] ]) AS pair
WITH pair[0] AS p1, pair[1] AS p2
CREATE (p1)-[:OWNS]->(p2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment