Reputation: 1820
I created many node with a few property has value is NULL (maybe its value is "") and now I want to remove all that property. I can't remove step by step property with its property name.
Update 1:
When I call match(u:User) return properties(u)
on Browser, it return that:
enter image description here
Upvotes: 1
Views: 893
Reputation: 21
I stumbled across this looking for elegant solutions. To optimize a query like this, even more, you can wrap it in an APOC procedure for batching in parallel like this:
CALL apoc.periodic.iterate(
"MATCH (u:User) RETURN u",
"WITH u, [key in keys(u) WHERE o[key] = '' | [key, null]] as nullifiers WHERE size(nullifiers) <> 0 WITH u, apoc.map.fromPairs(nullifiers) as nullifyMap SET u += nullifyMap",
{batchSize:30000, parallel:true, iterateList:true});
Upvotes: 2
Reputation: 30397
Ah, okay, these are empty string values, as you guessed.
We'll need a graph-wide query that will find nodes with empty-string properties. This will have to touch all properties of all nodes, so it may be expensive on a larger graph.
As for removing these dynamically, Cypher doesn't have great support for that kind of operation. If you can install APOC Procedures, however, then we can easily clear out these properties.
The approach I'm using in this query is to use a filter/extract on the keys of a node, and to create a pairing of a key to null
as a two-element list for all keys with empty string values.
We'll then use apoc.map.fromPairs()
to generate a map of those keys to null values, then use the +=
operation on the node, which will apply all these map values to the node's properties, effectively removing those properties since setting a property to null
is the same as removing it.
MATCH (u:User) // if you want this run for all nodes, remove the label
WITH u, [key in keys(u) WHERE u[key] = '' | [key, null]] as nullifiers
WHERE size(nullifiers) <> 0
WITH u, apoc.map.fromPairs(nullifiers) as nullifyMap
SET u += nullifyMap
Upvotes: 2