在我的Neo4j数据库中,我有以下实体:
@NodeEntity
public class Product {
private final static String CONTAINS = "CONTAINS";
private final static String DEFINED_BY = "DEFINED_BY";
private final static String VOTED_FOR = "VOTED_FOR";
private final static String PARENT = "PARENT";
private final static String CREATED_BY = "CREATED_BY";
@GraphId
private Long id;
@RelatedTo(type = PARENT, direction = Direction.INCOMING)
private Product parent;
@RelatedTo(type = CONTAINS, direction = Direction.OUTGOING)
private Set<Product> childProducts = new HashSet<>();
@RelatedTo(type = DEFINED_BY, direction = Direction.INCOMING)
private Set<Criterion> criterias = new HashSet<>();
@RelatedTo(type = VOTED_FOR, direction = Direction.INCOMING)
private Set<Vote> votes = new HashSet<>();
@RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
private User user;
}
@NodeEntity
public class Criterion {
private final static String CREATED_BY = "CREATED_BY";
private final static String DEFINED_BY = "DEFINED_BY";
@GraphId
private Long id;
@RelatedTo(type = DEFINED_BY, direction = Direction.OUTGOING)
private Product owner;
@RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
private User user;
}
@NodeEntity
public class Vote {
private static final String VOTED_ON = "VOTED_ON";
private final static String VOTED_FOR = "VOTED_FOR";
private static final String CREATED_BY = "CREATED_BY";
@GraphId
private Long id;
@RelatedTo(type = VOTED_FOR, direction = Direction.OUTGOING)
private Product product;
@RelatedTo(type = VOTED_ON, direction = Direction.OUTGOING)
private Criterion criterion;
@RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
private User user;
}
Product
是一个复合实体,可以包含子Products
。从层次结构中的某个Product
节点开始,我需要删除此Product
上的所有Votes
,然后我需要递归删除所有子Products
,Criteria
由这些节点和Votes
定义。 不得删除User
节点。
我尝试过这个密码查询:
MATCH (p:Product)-[r:CONTAINS*]-(e) WHERE id(p) = {productId} FOREACH (rel IN r| DELETE rel) DELETE e
但它仅删除产品,而不会删除开始节点上的投票以及所有子条件和投票。请帮助我进行正确的密码查询。谢谢。
我会把它分成两个查询。第一个递归向下收集产品层次结构,第二个删除一个产品节点及其直接环境。
获取产品层次结构很简单:
MATCH (p:Product)-[:CONTAINS*]->(childProduct)
WHERE id(p) = {productId}
RETURN id(childProduct) as id
要删除一个产品,我们需要删除该产品节点的所有关系。此外,如果所有相关节点既不是用户(您想保留它们)也不是产品(想想父产品 - 它们也应该保留),也需要删除它们。此外,我们需要确保目标节点未连接,这就是第二个可选匹配的原因:
MATCH (p:Product)
OPTIONAL MATCH (p)-[r]-(t)
WHERE id(p) = {productId}
DELETE r,p
WITH t
OPTIONAL MATCH (t)-[r2:VOTE_ON|:CREATED_BY]->()
WHERE none(x in labels(t) WHERE x in ["User", "Product"])
DELETE t,r2
我没有自己测试这个查询,因为你没有提供测试图。因此,请将其视为一个想法并对其进行修改,直到它起作用。
更新
在聊天中,我们发现这个密码语句解决了这个问题,请注意,Product
已被模型中Decision
标签替换:
MATCH (p:Decision)
WHERE p.name = "NoSQL"
WITH p
OPTIONAL MATCH (p)-[r]-(t)
DELETE p,r
WITH t,r
OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-()
WITH t, r2,r
WHERE none(x in labels(t) WHERE x in ["User", "Decision"])
DELETE t,r2