How to query property value when property name is a parameter?

自古美人都是妖i 提交于 2019-12-13 15:26:47

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/48912487/how-to-query-property-value-when-property-name-is-a-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!