Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove Neo4j nodes with duplicate properties?

Tags:

neo4j

cypher

In Neo4j 2.1.6, I have nodes that are non-unique in respect of a certain property, inputID.

Using Cypher, how do I remove all nodes that are duplicates in terms of a given property, leaving only uniques?

I have tried the following...

MATCH (n:Input)
WITH n.inputID, collect(n) AS nodes
WHERE size(nodes) > 1
FOREACH (n in tail(nodes) | DELETE n)

...but it results in...

Expression in WITH must be aliased (use AS) (line 2, column 6)
"WITH n.inputID, collect(n) AS nodes"
      ^

Thanks,

G

like image 709
gallygator Avatar asked Feb 27 '15 17:02

gallygator


People also ask

How do I get rid of duplicate nodes in Neo4j?

To delete all but one of the duplicate nodes, you could do this: MATCH (b:BusinessBranch) WITH b. address AS address, COLLECT(b) AS branches WHERE SIZE(branches) > 1 FOREACH (n IN TAIL(branches) | DETACH DELETE n);

How do I remove a property from Neo4j?

Remove a property Neo4j doesn't allow storing null in properties. Instead, if no value exists, the property is just not there. So, REMOVE is used to remove a property value from a node or a relationship. The node is returned, and no property age exists on it.

How delete all nodes and relationships in Neo4j give example?

There is a method to delete a node and all relationships related to that node. Use the DETACH DELETE statement: Example: MATCH (Kohli:player{name: "Virat Kohli"}) DETACH DELETE Kohli.

How do I delete a particular relationship in Neo4j?

In Neo4j to delete a node or relations between nodes you have to use DELETE clause. To delete any node you need DELETE clause with the MATCH statement, the MATCH statement data will find the specific node and whichever node is matched with the statement that node will be vanished.


1 Answers

You're not aliasing that WITH variable. Change this:

WITH n.inputID, collect(n) AS nodes

To this:

WITH n.inputID AS inputID, collect(n) AS nodes
like image 76
FrobberOfBits Avatar answered Sep 19 '22 15:09

FrobberOfBits