Dynamically remove node properties that are set to the string 'NULL'

I wish to run a query for a selection of nodes, to find every property that has a fake null value and just remove it. So for example I have data where some property keys, like 'fax' and 'postalCode', have been assigned the value 'NULL'. I want to completely remove each such key/value pair so that the value will actually be null as in no value assigned to that key on this node".

I found this article where a simple technique worked a few years ago but seems to no longer work today: Neo4j Cypher update node dynamic properties - Stack Overflow

In my attempt here, you can see that I am able to locate and express which keys for which nodes need to be removed or nullified...


...But if I uncomment that SET line, it fails due to a syntax error at the square brackets.


Is there a way to achive this in a fully dynamic manner? I have not explored apoc yet but hoping apoc is not necessary.

And by the way, this FOREACH fails with the exact same error.


It seems I *can* dynamically name the keys for display purposes but not for updates.

Hi @mojo2go ,

You can try something like:

CALL apoc.periodic.commit(
  "MATCH (c:Customer)
   UNWIND keys(c) as key
   WITH c, key WHERE c[key] = 'NULL' 
   with c, collect(key) as propertyKeys limit $limit
   CALL apoc.create.removeProperties(c, propertyKeys)
   yield node
   RETURN count(*)",
  {limit:100});

Lemme know if it works as expected on your db.

See ya

H

That works, and is easy to follow with a procedure named for just that operation. That said, I would like to wait a day to see if there is a non-APOC solution for this. I said I had hoped for a non-APOC solution and that's because every 'external' procedure I use has to be vetted and then set up for inclusion in our Neo4j build. Most likely I'll click Solution on this answer. Thanks Harold.

Hi @mojo2go !

Sorry if I forgot the APOC avoidance request. I have surfed many other related questions on the community but all roads lead to APOC. Good thing is that after you surpass the external procedure examination, you get a vast set of powerfull tools with Apoc.

Kinda hard to build something with a knife and a hammer unless you are Macgyver. :slight_smile:

H

Sorry to put you through that Harold. I clicked Solution.

And by the way I do love APOC. The APOC library set is a good model. It's different from other library systems because it's so close to the core product that it blurs boundary lines in a sense. In general the APOC procedures and functions are written by professionals with years of experience building Neo4j libraries, and paid by Neo4j. In addition APOC puts it out there and allows for mass useage of these functions which gives us that 'open source' battle testing. Neo4j is slow to cement them into the core product which forces the use of APOC. For example between the built in text function replace() and apoc.text.replace() I nearly always have to choose the latter. So is APOC less trustworthy than the core Neo4j codebase?.. Theoretically it is, but I haven't seen a clear difference in daily use. I automatically pull in all of APOC for any side project I do. And Aura, the most enterprise facing platform for Neo4j, automatically includes APOC. It can make sense to have a small core and a large ecosystem, like Linux, React, etc. but it can sometimes be a speedbump when dealing with the enterprise security guy or even just the guy in charge of the 'code base' which may not even include the database code. Regardless I couldn't live without APOC.
Cheers.