How to query property value when property name is

2019-07-24 16:45发布

Typically we can query property value for something like:

Match (n:Product)
where n.name="iPhone X"
return n

However, in my case, I don't know which property I should match, but I only know the value, in which case the property name becomes a kind of variable. I want something like this:

Match (n:Product)
    where n.{name}="iPhone X"
    return n

or with relationship variable r:

Match (n:Product)-[{r}]->(c:Color {name:'white'})

In both cases, in my application I know some property or relationship value beforehand, without knowing specifically which property it should match against.

Is this query based on property or relationship values supported in Neo4j or spring-data-neo4j?

标签: neo4j cypher
1条回答
迷人小祖宗
2楼-- · 2019-07-24 17:33

Take a look in this example:

CREATE (:Node {name : 'Bruno'})-[r:REL_TYPE]->()

Setting Neo4j Browser parameters:

:param {prop : 'name', rel_type : 'REL_TYPE'}

Then querying:

MATCH (n:Node) 
WHERE n[{prop}] = "Bruno"
RETURN n

The result:

╒════════════════╕
│"n"             │
╞════════════════╡
│{"name":"Bruno"}│
└────────────────┘

That is: you can use the property name as a key enclosed in square brackets in the WHERE clause.

An workaround to query by dynamic relationship types can be using the type() function, this way:

MATCH (:Node)-[r]->()
WHERE type(r) = {rel_type}
return r
查看更多
登录 后发表回答