neo4j - Cypher 选择任何邻居不包含属性的顶点
问题描述
{
"identity": 7,
"labels": [
"Parent"
],
"properties": {
"name": "foo1"
}
},
{
"identity": 8,
"labels": [
"Child"
],
"properties": {
"name": "bar2"
}
},
{
"identity": 9,
"labels": [
"Child"
],
"properties": {
"name": "bar1"
}
},
{
"identity": 10,
"labels": [
"Parent"
],
"properties": {
"name": "foo2"
}
}
我想选择没有孩子的父母name='abc'
预期行为:我应该得到父母 (foo1
和foo2
) 作为结果
查询1:
Match (x1:Parent) with x1
optional Match (x1)-[:CHILD]-(x2:Child) with x1 , collect(x2) as x3
UNWIND x3 as x2
WITH x1 , x2 , x3
where none (x IN x3 where x.name IN ['abc'])
return DISTINCT x1
这个查询只返回 1 个 Parent( foo1
),但它应该返回两个父母,并且第二个 parent( foo2
) 没有连接到任何孩子。
PS:使用的原因UNWIND
是将变量用于变量的进一步WHERE
子句x2
。
解决方案
这将得到所有没有孩子名字的父母:'abc'或没有任何孩子
MATCH (p:Parent)
WHERE NOT EXISTS((p)-[:CHILD]->(:Child {name: 'abc'}))
RETURN p
检查给定的父母 p 是否有一个未命名的孩子:'abc'
========== 已编辑:
我个人不喜欢这个查询,因为它正在收集 x2 然后稍后展开。但为了回答 SO 问题,
Match (x1:Parent) with x1
optional Match (x1)-[:CHILD]->(x2:Child) with x1 , collect({x1:x1, x2:x2}) as x3
UNWIND x3 as x2
WITH x2.x1 as x1, x2.x2 as x2, x3
where NOT x2.name IN ['abc'] OR x2 is NULL
RETURN distinct x1
我收集了 x1 和 x2,然后通过在展开期间获取 x1 和 x2 来展开集合。这将包括没有 x2(没有 CHILD)的其他节点。我还简化了 where 条件,因为 x2 也可以访问,而不是在 x3 上再次循环
推荐阅读
- hybris - Hybris 启动服务器进行单元测试
- macos - 如何使用 Homebrew 安装 solr 6.x?
- google-cloud-dataflow - 如何使用其他命令行参数显式设置直接运行器?
- janus-gateway - 在重新协商时使用自己的流
- sql-server - 你能在 TSQL 中引用变量标识符和参数标识符吗?
- javascript - 如何获得 Highmaps 缩放级别以及是否有 Highmaps 缩放事件?
- angular - 商店更新时重新渲染 HTML - NGXS
- git - 使用 Git 子模块获得类似 `svncopy --pin-externals` 行为的最简单方法
- tomcat - Tomcat 客户端和第一个 ignite 服务器之间的通信问题
- android - 如何使用 gradle 为风味配置不同的 buildType