Skip to content

Instantly share code, notes, and snippets.

@jexp
Last active August 29, 2015 14:01
Show Gist options
  • Save jexp/b01ae5a9a863e126c116 to your computer and use it in GitHub Desktop.
Save jexp/b01ae5a9a863e126c116 to your computer and use it in GitHub Desktop.
creating a sample graph

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

FOREACH (name in ["Amanda","Michael", "Max","Magnus","Mark","Peter","Andres"] |
  CREATE (:User {name:name}))

Connecting users starting with M

MATCH (u:User)
    WHERE u.name =~ 'M.*'
    WITH collect(u) as users
    foreach ( i in range(1,length(users)-1) |
      foreach (u1 in [users[i-1]] | foreach ( u2 in [users[i]] |
        create (u1)-[:knows]-> (u2) ))
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment