在 Neo4j 中创建具有属于另一个结构一部分的节点的独特结构

Creating unique structures in Neo4j with them having nodes that are part of another structure

假设我们有 n 个标签为 :Test 的节点和一个名为 type.属性 的唯一节点。

UNWIND[{ type:"a" }, { type:"b" }, { type:"c" }, { type:"d" }] AS x
MERGE (t:Test { type: x.type })
RETURN t

看起来像这样

现在介绍一个label节点:Collection。此节点的目的是与 :Test 节点具有唯一的关系模式。

MATCH (a:Test { type:"a" }),(b:Test { type:"b" })
CREATE UNIQUE (x:Collection)-[:HAS]->(a),(x:Collection)-[:HAS]->(b)
Return *

当我尝试制作另一个独特的结构时,我遇到的问题开始出现,就像以前的结构一样,但有一些共同的节点。

MATCH (a:Test { type:"a" })
CREATE UNIQUE (x:Collection)-[:HAS]->(a)
RETURN *

预期的结果是标签 :Collection 的另一个节点被创建并链接到 :Test {type :"a"} 但实际结果是它匹配以前的数据结构和 returns 而不是创建一个新的。

预期结果应该有 2 个 :Collection 节点,一个链接到 type:"a",另一个链接到 类型:"a"类型:"b".

任何类型的输入都将不胜感激:D

来自neo4j docs on CREATE UNIQUE:

CREATE UNIQUE is in the middle of MATCH and CREATE — it will match what it can, and create what is missing. CREATE UNIQUE will always make the least change possible to the graph — if it can use parts of the existing graph, it will.

您添加 Collection 个没有任何属性的节点。我认为如果 CREATE UNIQUE 找到一个 Collection 节点,它将使用它。这就是 CREATE UNIQUE 应该工作的方式。

因此,如果您想要一个链接到某些 Test 节点的新 Collection,您可以向该节点添加一些独特的属性:

MATCH (a:Test { type:"a" })
CREATE UNIQUE (x:Collection {key: 'unique value'})-[:HAS]->(a)
RETURN *

或在单独的步骤中创建它:

MATCH (a:Test { type:"a" })
CREATE (x:Collection)
CREATE (x)-[:HAS]->(a)
RETURN *

或使用MERGE代替CREATE UNIQUE