是否可以在不使用APOC的情况下使用UNWIND动态设置cypher中的标签?

问题描述 投票:0回答:1

这是我的查询:

def create_entity_nodes(tx, report_id, entities):

    query = (
        "UNWIND $entities AS entity "
        "MATCH (report:Report {id: $report_id}) "
        "CREATE (entity.name:entity.type entity), "
        "(report)-[:OWNS]->(entity.name) "
        "(entity.name)-[:BELONGS_TO]->(report)"
        "RETURN report"
    )

我之前只对下面代码中的一个实体进行过操作,并且它有效。所以,我想尝试用一个列表,但我不确定是否可能。

    entity_type = entity["type"]
    if not entity_type:
        raise ValueError("Entity type is required.")
    entity_type = entity_type[0].upper() + entity_type[1:].lower()
    entity.pop("type")
    query = (
        f"MATCH (report:Report {{id: $report_id}}) "
        f"CREATE ({entity['name']}:{entity_type} $props), "
        f"({entity['name']})-[:BELONGS_TO]->(report), "
        f"(report)-[:OWNS]->({entity['name']})"
        f"RETURN report"
    )
    result = tx.run(query, report_id=report_id, props=entity)
python neo4j cypher
1个回答
0
投票

不,您的第一个查询将不起作用。目前,动态创建标签的唯一方法是像第二个查询一样构建查询字符串,或者使用像

apoc.create.node
这样的 apoc 函数。

© www.soinside.com 2019 - 2024. All rights reserved.