1
votes

I'm looking for a query which can find all of the nodes which have properties that match a parameter which is an object. The properties of the node can be a superset (contain more properties than the filter)

e.g.

:param obj : { first : "first" }

CREATE (n:Test { first : "first", second : "second" });
CREATE (m:Test { first : "first" });
CREATE (f:Fail { first : "bad", second : "second" });
MATCH (c)
    WHERE
        PROPERTIES(c) = $obj
RETURN c;

n and m should be returned as they are matching on first : "first"

1
Are you obliged to do this with Cypher? When I encounter this kind of problem, I usually can build the query with another language, and then execute it from a Neo4j driver for this language.stellasia
@stellasia I'm not sure how building it in another language helps, I'd rather not risk introducing any injections. I'd also rather develop my knowledge of cypher so that if I do need to directly query my graph, I don't have to go a round about way and do it through another language. There's also always the possibility that query builders are out of date or no longer developed.A. L

1 Answers

2
votes

it is doable with apoc, basically by matching the obj with a submap of the properties, containing only the keys that are also present in obj

WITH { first : "first" } AS obj
MATCH (c)
WHERE apoc.map.submap(properties(c),keys(obj),[],false)= obj
RETURN c