Neo4j,匹配关系 WHERE AND

标签 neo4j cypher

您好,我正在尝试使用“WHERE AND”来匹配 Neo4j 关系

我的示例关系是:“用户访问国家/地区”

我是这样创建的...

MATCH (c:Country{Name:Country}) MERGE (u:User{Email:Email,UserID: UserID}) MERGE (u)-[r:Visits]->(c)
//Countries are previously created and Users may or may not exist

然后我查询(This Works):
MATCH (u:User)-[r:Visits]->(c:Country) where c.Name='France' or c.Name='Spain' return u

结果:显示所有访问过西类牙或法国的用户,即使他们只访问过两个国家之一。

但是我试图做的是完全相同的查询,但使用“AND”而不是“OR”。我可以在其中获得访问过“法国”和“西类牙”的用户。
MATCH (u:User)-[r:Visits]->(c:Country) where c.Name='France' AND c.Name='Spain' return u

结果:找到 0 个节点和关系..

我能做什么?

最佳答案

在您的查询中,您匹配单个国家/地区节点并说该节点的名称必须是 France并且必须是 Spain .

您想要的是找到所有访问过法国和西类牙的用户。有几种方法可以走...

//match both countries against the same user and identify them separtely
//making two matches in a single query
MATCH (u:User)-[:VISITS]->(c1:Country), (u)-[:VISITS]->(c2:Country)
WHERE c1.name = "France"
AND c2.name = "Spain"
RETURN u.name

//match all users that have been to either and only return the one that have been to both
MATCH (u:User)-[r:VISITS]->(c:Country) 
WHERE (c.name IN [ "France", "Spain"])
WITH u, count(*) AS num
WHERE num = 2
RETURN u.name, num 

它认为第一更好,因为它更精确,可能更有效。

关于Neo4j,匹配关系 WHERE AND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27433808/

相关文章:

neo4j - NEO4J 中最多可以拥有多少个节点?

java - 使用 Cypher 在嵌入式 Neo4J 应用程序中创建节点

neo4j - 从 COLLECT 子句的输出中删除重复项

neo4j - 密码查询,加载 CSV 在 Neo4j 2.1 中没有响应

java - Neo4j java 驱动程序的 Cypher 查询执行时间

Spring 数据 Neo4j : persist() method undefined

performance - 无法重现/验证图形数据库中的性能声明和行动书中的 neo4j

java - Neo4j:无法实例化 BatchInserter

neo4j - 获取两个节点之间的所有路由 neo4j

neo4j - 更新至: Adding node to Neo4j Spatial Index