【问题标题】:Cypher select vertices whose any neighbours do not contain a propertyCypher 选择任何邻居不包含属性的顶点
【发布时间】:2021-06-08 10:56:19
【问题描述】:

{
    "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'

选择没有任何孩子父母

预期行为:我应该得到父母(foo1foo2)作为结果

查询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 个父母(foo1),但它应该返回父母和第二个父母(foo2)没有连接到任何孩子。

PS : 使用 UNWIND 的原因是为了将变量用于变量 x2 上的更多 WHERE 子句。

【问题讨论】:

  • 当您读取数据模型时,它会读取 Foo1 is Child of Bar1 而 Bar2 则相反。它应该读作 Foo1 is a PARENT of Children Bar1 和 2。
  • 其实我是想暗示 foo1 和 foo2 是父母,而 bar1 和 bar2 实际上是 foo1 的孩子。
  • 那么关系名称应该是 (:Parent) - [:HAS_CHILD] -> (:Child)
  • 是正确的???? .它应该是 HAS_CHILD

标签: neo4j cypher


【解决方案1】:

这将得到所有没有孩子名字的父母:'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 的其他节点 (没有孩子)。我还简化了 where 条件,因为 x2 也可以访问,而不是在 x3 上再次循环

【讨论】:

  • 不这样做的原因是因为我想在子变量上编写许多复杂的查询,例如在某个范围内的年龄。而且我真的很想知道我的查询出了什么问题
  • 请给我完整的要求,以便我得到完整的图片。
  • 尝试将 'abc' 更改为 'bar2' 并且您的查询也会返回 foo1,这是不正确的。它应该只返回 foo2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
相关资源
最近更新 更多