gpt4 book ai didi

neo4j - Cypher 选择其任何邻居不包含属性的顶点

转载 作者:行者123 更新时间:2023-12-05 06:49:39 27 4
gpt4 key购买 nike

enter image description here

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

我想选择 Parents 没有任何 childname='abc'

预期行为:我应该得到两个 parent (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 子句。

最佳答案

这将得到所有没有 child 名字的 parent :'abc' 或没有任何 child

MATCH (p:Parent)
WHERE NOT EXISTS((p)-[:CHILD]->(:Child {name: 'abc'}))
RETURN p

Check if the given parent, p has a child not named: '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

I collected BOTH x1 and x2 then UNWIND the collection by getting x1and x2 during unwind. This will include other nodes that has no x2(without a CHILD). I also simplified the where condition because x2 is also accessible rather than looping again on x3

关于neo4j - Cypher 选择其任何邻居不包含属性的顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66565759/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com