title: Neo4j: Check condition between all consecutive relations in path
body: <p>I want to query all paths of arbitrary length</p>
  
  <pre><code>(a)-[:Relation*]-&gt;(b)
  </code></pre>
  
  <p>where for each pair <code>(r1, r2)</code> of consecutive relationships</p>
  
  <pre><code>()-[r1]-&gt;()-[r2]-&gt;()
  </code></pre>
  
  <p>a condition, say <code>r2.foo &gt; r1.foo</code>, is met.</p>
  
  <p>You can imagine <code>foo</code> being a timestamp and I only want to find paths where all steps are in chronological order.</p>
  
  <hr>
  
  <p>What I currently have is this:</p>
  
  <pre><code>MATCH path = ()-[:Relationship*]-&gt;()
  WITH rels(path) AS rels, path
  WHERE reduce(acc = { inOrder: true, previous: rels[0] }, r IN tail(rels) |
      { inOrder: acc.inOrder AND r.foo &gt; acc.previous.foo, previous: r }).inOrder
  RETURN path
  </code></pre>
  
  <p>Now, this seems to work. However, I'm not sure if this is the most efficient query. Is there a better way to do it? </p>