Search code examples
neo4jcyphergraph-databasesmemgraphdbopencypher

Regular expression on the string of a property name in Cypher


I know it is possible to use regular expressions for property values like for example:

MATCH (n)
WHERE n.SomeProperty =~ 'somestring*'
RETURN n;

What i want is to use regular expression on the property name and check for all the properties which start with a certain string like for example:

MATCH (n)
WHERE n.`SomeProperty*` > 10
RETURN n;

So I want to have all nodes which have a property which begins with 'SomeProperty' and have a value > 10 for this property.

This doesn't seems possible with using regular expressions like in my example. I've tried it and with my research i couldn't find a solution. Does anyone have an idea how to achieve this using another technique ?


Solution

  • Given the following test graph

    CREATE (:TestNode {somePropertyOne: 10})
    CREATE (:TestNode {somePropertyTwo: 11})
    CREATE (:TestNode {somePropertyThree: 12})
    CREATE (:TestNode {someOtherProperty: 13})
    

    The following query achieves what you want

    MATCH (n)
    WHERE ANY(x IN keys(n) WHERE x STARTS WITH 'someProperty' AND n[x] > 10)
    RETURN n
    
    ╒════════════════════════╕
    │"n"                     │
    ╞════════════════════════╡
    │{"somePropertyTwo":11}  │
    ├────────────────────────┤
    │{"somePropertyThree":12}│
    └────────────────────────┘
    

    Bear in mind that its really not an optimized query for graphs, so it will be slow on decent size databases.