title: How to return nodes that have only one given relationship
body: <p>I have nodes that represent documents, and nodes that represent entities. Entities can be referenced in document, if so, they are linked together with a relationship like that :</p>
  <pre><code>(doc)&lt;-[:IS_REFERENCED_IN]-(entity)
  </code></pre>
  <p>The same entity can be referenced in several documents, and a document can reference several entities.</p>
  <p>I'd like to delete, for a given document, every entity that are referenced in this given document <strong>only</strong>.</p>
  <p>I thought of two different ways to do this.
  The first one uses java to make a foreach and would basically be something like that :</p>
  <pre><code>List&lt;Entity&gt; entities = MATCH (d:Document {id:0})&lt;-[:IS_REFERENCED_IN]-(e:Entity) return e
  for (Entity entity : entities){
      MATCH (e:Entity)-[r:IS_REFERENCED_IN]-&gt;(d:Document) WITH *, count(r) as nb_document_linked WHERE nb_document_linked = 1 DELETE e
  }
  </code></pre>
  <p>This method would work but i'd like not to use a foreach or java code to make it. I'd like to do it in one cypher query.
  The second one uses only one cypher query but doesn't work. It's something like that :</p>
  <pre><code>MATCH (d:Document {id:0})&lt;-[:IS_REFERENCED_IN]-(e:Entity)-[r:IS_REFERENCED_IN]-&gt;(d:Document) WITH *, count(r) as nb_document_linked WHERE nb_document_linked = 1 DELETE e
  </code></pre>
  <p>The problem here is that nb_document_linked is not unique for every entity, it is a unique variable for all the entities, which mean it'll count every relationship of every entity, which i don't want.</p>
  <p>So how could I make a kind of a foreach in my cypher query to make it work?</p>
  <p>Sorry for my english, I hope the question is clear, if you need any information please ask me.</p>