Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

neo4j - Dynamic property keys used in Cypher Query to manipulate properties (regardless of the property values)

This is more of a database management question. I'm not trying to extract content stored in properties, but I am trying to clean up a messy database.

For example, I have multiple obsolete properties that start with "type", for example n.typePerson, n.type_Mangoes, n.typehouse, n.dvdTypes.

match (n) 
with keys(n) as key, n 
unwind key as k 
with distinct k, n
WHERE tolower(k) contains "type"
with n, k
return k, n[k] 

--> works fine

REMOVE n[k] return n{.*} 

--> error message

WITH distinct n, collect(k) as list
FOREACH (o IN list | REMOVE n[o])
return n{.*}

--> error message

I'm hitting a wall when it comes time to do anything with the property. I can return n[k] fine, but I cannot seem to set or remove n[k] without getting this error:

Neo.ClientError.Statement.SyntaxError: Invalid input '[': expected an identifier character, whitespace, node labels, '{', 'e/E', a property map, a relationship pattern, '.' or '(' (line 7, column 9 (offset: 136))
"REMOVE n[o]"

Most grateful for pointers. Thanks!

question from:https://stackoverflow.com/questions/65849630/dynamic-property-keys-used-in-cypher-query-to-manipulate-properties-regardless

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Indeed, this is one of the use cases for using the APOC library in Neo4j, to circumvent the limitations of Cypher where variables cannot be used : labels, relationship types, property keys.

The apoc.create.removeProperties procedure will help you with that :

WITH distinct n, collect(k) as list
CALL apoc.create.removeProperties([n], list)
YIELD node
return node{.*}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...