I have a requirement that in my database some nodes should have at most one relationship of a given type. For example, nodes with the label Heart
should be connected via has
to at most one node of label Body
.
Here's some test data to illustrate the issue:
CREATE (h:Heart {animal: "zebra"})
CREATE (b1:Body {animal: "zebra"})
CREATE (b2:Body {animal: "elephant"})
CREATE (b1)-[:has]->(h)
CREATE (b2)-[:has]->(h)
CREATE (h2:Heart {animal: "mouse"})
CREATE (b4:Body {animal: "mouse"})
CREATE (b5:Body {animal: "leopard"})
CREATE (b4)-[:has]->(h2)
CREATE (b5)-[:has]->(h2)
With the following query I can find all nodes with connections which don't respect this rule:
MATCH (n:Heart) WHERE (:Body)-[:has]-(n)-[:has]-(:Body) RETURN n
I'd like though, to write a statement to remove the "extra" relationships. Right now I've got a query to remove the nodes composing these "extra" relationships, but it also removes the nodes I'd like to keep (one of each).
MATCH (b1:Body)-[:has]-(n)-[:has]-(b2:Body) DETACH DELETE b1, b2
This also doesn't group relationships by the center node, so I can't really remove all nodes except one, as it would not have the desired effect.
Does anyone know how can I write a statement that will remove all "extra" relationships until only one relationship has
remains for each Heart
node? Preferably the oldest one.