首页 > 解决方案 > 如何将嵌套列表插入具有直接关系的 neo4j(密码查询语言)

问题描述

我想通过密码查询语言将多级列表插入 neo4j 4.1.1。

我要插入的数据是以 json 格式返回的报告列表。属性之一是报告背后的模型。我想为列表中的每个报告创建一个节点 + 为给定报告中的每个模型创建一个节点,并在报告和模型之间添加一个关系(在报告中给出)。这将是每日数据刷新,所以如果neo4j数据库中不存在新的报告节点,我想创建新的报告节点,如果报告已经存在,我想覆盖数据以防我们有新信息。

示例数据:

[
    {
        "name": "Report 1",
        "description": "Report Description",
        "createdBy": "TRAINING1",
        "id": "04CA095985D9A838E10000000A4E740E",
        "created": "2020-02-14T14:27:56.371Z",
        "changed": "2020-02-14T14:27:56.371Z",
        "isTemplate": false,
        "isSample": false,
        "models": [
            {
                "description": "Model 1",
                "id": "id of the model",
                "isPlanning": true
            },
            {
                "description": "Model 2",
                "id": "id of the model",
                "isPlanning": true
            },
            {
                "description": "Model 3",
                "id": "id of the model",
                "isPlanning": true
            },
          
        ],
        "changedBy": "TRAINING1",
        "openURL": "URL of the report"
    },
     {
        "name": "Report 2",
        "description": "Report Description",
        "createdBy": "TRAINING1",
        "id": "0653F758E5264C49E10000000A4E740E",
        "created": "2020-02-14T14:27:56.371Z",
        "changed": "2020-02-14T14:27:56.371Z",
        "isTemplate": false,
        "isSample": false,
        "models": [
            {
                "description": "Model description",
                "id": "id of the model",
                "isPlanning": true
            },
            {
                "description": "Model description",
                "id": "id of the model",
                "isPlanning": true
            },
            {
                "description": "Model description",
                "id": "id of the model",
                "isPlanning": true
            },
          
        ],
        "changedBy": "TRAINING1",
        "openURL": "URL of the report"
    },
    {
        "name": "Report Name 3",
        "description": "Report description 3",
        "createdBy": "TRAINING1",
        "id": "0653F758E5264C49E10000000A4E740E",
        "created": "2020-02-14T14:27:55.010Z",
        "changed": "2020-02-14T14:27:55.010Z",
        "isTemplate": false,
        "isSample": false,
        "models": [
            {
                "description": "description of the model",
                "id": "id of the model",
                "isPlanning": true
            },
            {
                "description": "model description",
                "id": "this is an id",
                "isPlanning": true
            },
            {
                "description": "model description",
                "id": "this is an id",
                "isPlanning": true
            }
        ],
        "changedBy": "TRAINING1",
        "openURL": "URL of the report"
    }
]

我想成为的图表如下: 示例图表

我尝试的查询如下:

UNWIND $propsArray as props
      MERGE (sac:sacreports { id: props.id })
        ON CREATE
          SET sac = {id: props.id, name: props.name }
          UNWIND props.models as model
          MERGE (m: sacmodels {id: model.id})
          ON CREATE SET m = {id: model.id, description: model.description }
          ON MATCH SET m = {id: model.id, description: model.description }
        ON MATCH 
          SET sac = {id: props.id, name: props.name, description: props.description }
          UNWIND props.models as model
          MERGE (m: sacmodels {id: model.id})
          ON CREATE SET m = {id: model.id, description: model.description }
          ON MATCH SET m = {id: model.id, description: model.description }
          
      RETURN sac

我收到以下错误:Neo4jError: WITH is required between MERGE and UNWIND(第 7 行,第 11 列(偏移量:181))。

关于如何执行此操作的任何建议?

提前致谢!

标签: neo4jcypher

解决方案


我认为ON CREATE并且ON MATCH只支持一个SET子句。而且您永远不会在查询中创建报表和模型之间的关系。如果您通过节点的 id 进行合并,那么您不想在ON CREATE语句中覆盖它。我会使用以下查询:

UNWIND $propsArray as props
      MERGE (sac:sacreports { id: props.id })
        ON CREATE SET sac += {name: props.name , description: props.description}
      WITH sac, props
      UNWIND props.models as model
          MERGE (m: sacmodels {id: model.id})
          ON CREATE SET m += {description: model.description }
          MERGE (sac)-[:IS_USED_IN]->(m)

推荐阅读