4

I'm searching for a way to remove every property, of any node in the DB, having a specific value using Cypher.

Context
I got a csv bulk file from a relational table with plenty of NULL values. LOAD CSV brings them as values. Removing them (replacing them with empty '' within the csv file) resulted in the same issue (properties without values). Tried many (many) Cypher operations to discard NULL values but nothing worked.

Can't find anything in the docs neither by Googling. Can this be done using only Cypher? It seems to me not (yet) supported.

Thanks.

Nadjib Mami
  • 5,736
  • 9
  • 37
  • 49
  • 1
    Do you not know the property names? Whatever you did when you replaced null values with empty strings (you must have matched the right nodes and properties somehow), can you do that again but do `REMOVE n.property` instead of `SET n.property = ''`? – jjaderberg Jun 30 '14 at 10:51
  • I replaced 'NULL' by '' in the csv file (find and replace). I said maybe Cypher detects the empty strings and skip the corresponding attributes, but it wasn't the case. – Nadjib Mami Jun 30 '14 at 11:12
  • So you want to remove any property on any node that is an empty string? – Nicole White Jun 30 '14 at 11:31

2 Answers2

5

How about this (when you know the property-names):

MATCH (n:Label)
WHERE n.property = ''
REMOVE n.property;

MATCH (u:User) 
WHERE u.age = '' 
SET u.age = null;

If you know which columns these are in your import you can do something like this

load csv with headers from "" as line
with line, case line.foo when '' then null else line.foo end as foo
create (:User {name:line.name, foo:foo})

It won't create the properties with null.

For numeric values it's easier as toInt() and toFloat() return null on unparseable values like ''.

Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • 1
    There are many columns, it would be hard to do the work for each one. But since there is no other way, then I'll accept. Thanks @Michael. BTW, I appreciate your notable effort in SO and the Google group. – Nadjib Mami Jun 30 '14 at 15:29
  • Use your programming language of choice to loop through the header row of your CSV and generate the necessary CASE statements. – Nicole White Jun 30 '14 at 16:51
0

No, there is no way to do this only with chypher. I suppose that you have already seen the way to do it via REST. That is the best solution for now.

Eduardo Páez Rubio
  • 1,032
  • 2
  • 9
  • 31
  • No, I didn't yet played with REST requests, but this did not answer the question. How to specify the value of the properties to delete? E.g. delete all the properties with "foo" as value? – Nadjib Mami Jun 30 '14 at 10:35
  • 1
    I think the question is about removing properties that have a particular value, no matter what node they are on, not remove all properties on a node. To remove all properties on a node `n` with cypher you can use `SET n = {}`. – jjaderberg Jun 30 '14 at 10:47