gpt4 book ai didi

java - 如何使用SDN仅在节点不存在时创建节点?

转载 作者:行者123 更新时间:2023-12-01 13:17:52 24 4
gpt4 key购买 nike

使用SDN Neo4j,是否可以仅在匹配字段的节点不存在时才创建节点?例如,我有一个现有节点,可以说标记为“Road”,值为“N4”。现在我需要创建一个与该节点有关系的新节点,可以说“继续行驶”。所以我做得很好。然而,第二次我需要检查标签为“Road”的节点是否已存在且值为“N4”。如果确实如此,则使新节点指向现有节点,而不是创建值为 N4 的新 Road 节点?

我正在尝试使用 SDN 做同样的事情,我认为:

MATCH (root { name: 'root' })
CREATE UNIQUE (root)-[:X]-(leaf { name:'D' })
RETURN leaf

与根节点连接的节点都没有名称为 D,因此创建一个新节点来匹配该模式。

这可以在插入点使用 SDN 域建模来完成吗?

谢谢

最佳答案

总的来说,按需修建道路可能不是一个好主意?

其实很简单

class Road {
@Indexed(unique=true) String name;
}

class Person {
@Indexed(unique=true) String name;
Road drivesOn;
}

Road r = template.save(new Road("N4")); // does a merge if the road already exists, but of course more expensive

Person p = new Person("Timmy")
p.setDrivesOn(r);

// also merges person if it doesn't exist,
// the default is that there is only one relationship ever between this person and _a_ road.

template.save(p);

关于java - 如何使用SDN仅在节点不存在时创建节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22320042/

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