简体   繁体   English

检查Neo4J中2个节点之间是否存在关系,如果没有,则创建具有随机ID的节点

[英]Check if relationship exits between 2 nodes in Neo4J and if not create node with random id

I have UserTypeA node and UserTypeB node and I want to connect them to a User node that will be used now only to include random ID. 我有UserTypeA节点和UserTypeB节点,我想将它们连接到User节点,该节点现在仅用于包含随机ID。 is it possible to check if a relationship exits between 2 nodes (to check if UserTypeA already has a user node that is connect to him) and if not, to create a new User node and set a random id for this node (is it even possible to set a random id?). 是否可以检查2个节点之间是否存在关系(以检查UserTypeA是否已经有一个与其连接的用户节点),如果没有,则创建一个新的User节点并为此节点设置一个随机ID(甚至可以设置一个随机ID?)。 Is there a way to do it in one query? 有没有办法在一个查询中做到这一点? (if a relationship not exist, create new User node and set a random id for it) (如果不存在关系,请创建新的“用户”节点并为其设置随机ID)

Thanks 谢谢

You can do this very straightforward, almost converting your English into Cypher: 您可以非常简单地执行此操作,几乎可以将英语转换为Cypher:

MATCH (a:User {name:"userA"}),(b:User {name:"userB"})
WHERE NOT (a)-[:KNOWS]-(b)
WITH a,b
CREATE (c:User {name:"userC",id:rand()})
CREATE (a)-[:KNOWS]->(c)
CREATE (b)-[:KNOWS]->(c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM