title: Neo4j Cypher: Merge duplicate nodes
body: <p>I have some duplicate nodes, all with the label Tag. What I mean with duplicates is that I have two nodes with the same name property, example:</p>
  
  <pre><code>{ name: writing, _id: 57ec2289a90f9a2deece7e6d},
  { name: writing, _id: 57db1da737f2564f1d5fc5a1},
  { name: writing }
  </code></pre>
  
  <p>The <code>_id</code> field is no longer used so in all effects these three nodes are the same, only that each of them have different relationships.</p>
  
  <p>What I would like to do is:</p>
  
  <ol>
  <li><p>Find all duplicate nodes (check)</p>
  
  <pre><code>MATCH (n:Tag)
  WITH n.name AS name, COLLECT(n) AS nodelist, COUNT(*) AS count
  WHERE count &gt; 1
  RETURN name, nodelist, count
  </code></pre></li>
  <li><p>Copy all relationships from the duplicate nodes into the first one</p></li>
  <li>Delete all the duplicate nodes</li>
  </ol>
  
  <p>Can this be achieved with cypher query? Or do I have to make a script in some programming language? (this is what I'm trying to avoid)</p>