Tar*_*eeb 1 path neo4j graph-databases cypher
我想从路径中删除一个节点,而不会危及原始路径节点.
这是我的测试数据库:
我想从路径中删除node(2),但我希望节点1,3,4和5在路径中保持链接.
有没有办法在一个查询中执行此操作?到目前为止,我有以下内容:
MATCH p = (:Connect)-[:to*]-(:Connect)
WITH nodes(p) AS connectNodes
UNWIND connectNodes AS connectNode
WITH distinct connectNode
WHERE connectNode.connectID = 2
DETACH DELETE (connectNode)
Run Code Online (Sandbox Code Playgroud)
这将删除节点2并取消链接路径
如何在没有节点2的情况下维护原始路径的节点之间的链接?
编辑:
我通过修改接受的答案的答案来解决它
//Make sure node (n) belongs to the path
MATCH (n:Connect {cID:2})-[:to*]-(:Connect {cID:5})
//get attached nodes, if any, ignoring directions
OPTIONAL MATCH (oa:connect)-[:to]-(n)-[:to]-(ob:connect)
//make sure nothing is duplicated
WHERE oa.cID <> ob.cID
//Use FOREACH to mimic if/else. Only merge oa to ob if they exist. Query fails without it
FOREACH (_ IN case when oa IS NOT NULL then [true] else [] end |
MERGE (oa)-[:to {created: 1542103211}]-(ob)
)
//Get n, and get all connected nodes to it, and delete the relationship(s)
WITH n
OPTIONAL MATCH (n)-[r:to]-(:Connect) DELETE r
Run Code Online (Sandbox Code Playgroud)
这是最简单的方法,即匹配将要中断的路径,然后在删除节点的同一个cypher中创建新链接.
// Match the target node for deletion
MATCH (n{id:2})
// Match broken paths, if any
OPTIONAL MATCH (a)-[ra]->(n)-[rb]->(b)
// Create new link to replace destroyed ones
CREATE (a)-[r:to]->(b)
// Copy properties over, if any
SET r+=ra, r+=rb
// Remove target node
DETACH DELETE n
// If you want to keep the node and just disconnect it, replace last line with
// OPTIONAL MATCH (n)-[r:to]-() DELETE r
Run Code Online (Sandbox Code Playgroud)
有APOC如果你想要的类型从删除关系之一抄你可以用它来创建一个动态型的关系的功能.
| 归档时间: |
|
| 查看次数: |
217 次 |
| 最近记录: |