简体   繁体   中英

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. 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?). 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)

Thanks

You can do this very straightforward, almost converting your English into 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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