title: Create unique linked list nodes/relationships in Neo4j
body: <p>I'm implementing basic visitor metrics tracking in Neo4j (forgive me; I'm new to graph databases in general).  I have three node types: Visitor, Session, and Event.</p>
  
  <p>Every time I create a new Event node, I'd like to also create new Session and/or Visitor nodes, if necessary (nodes with the same IDs do not yet exist).  Here's what I've tried:</p>
  
  <pre><code>CREATE UNIQUE
  (e:Event { type: 'pageview' })&lt;-[:ACTION]-(s:Session { id: 'ABC'})&lt;-[:SESSION]-(v:Visitor { id: '123' })
  RETURN e,s,v;
  </code></pre>
  
  <p>This gives the following error: <code>Unbound pattern!</code>, which makes sense, as I haven't explicitly bound anything to a reference node.  However, I'm hoping to write the query in a way that leverages whatever exists in the indexes, and creates whatever is missing.</p>
  
  <p>I'm using version 2.0 (with labels), and have created the following indices:</p>
  
  <pre><code>CREATE INDEX on :Session(id);
  CREATE INDEX on :Visitor(id);
  </code></pre>
  
  <p>Additionally, I'd like to implement linked lists for the Events in a Session and the Sessions for a Visitor.  Basically, if a preexisting [:ACTION] relationship exists, delete the old relationship and relate the new Event node to the previous Event node (for that same session).  The same goes for Sessions and Visitors with their [:SESSION] relationship, ie:</p>
  
  <pre><code>curSession-[:ACTION]-&gt;curEvent-[:PREV]-&gt;prevEvent...
  visitor-[:SESSION]-&gt;curSession-[:PREV]-&gt;prevSession...
  </code></pre>
  
  <p>I could probably figure out how to do this with multiple queries, but I'm trying to learn how correctly to leverage a graph database structure (and I'm concerned about race conditions).</p>