【问题标题】:Neo4j Python Driver Using Unwind with a list of dictionariesNeo4j Python 驱动程序使用 Unwind 和字典列表
【发布时间】:2022-12-03 13:53:04
【问题描述】:

我正在尝试批量合并以创建多个节点。使用下面的代码,

def test_batches(tx,user_batch):
            result= tx.run(f"Unwind {user_batch} as user\
                           MERGE (n:User {{id: user.id, name: user.name, username: user.username }})")

但是我收到了这个错误。 注意我正在传递一个字典列表。

CypherSyntaxError: {code: Neo.ClientError.Statement.SyntaxError} {message: Invalid input '[': expected "+" or "-" (line 1, column 8 (offset: 7))
"Unwind [{'id': 1596859520977969156, 'name': 'Bigspuds', 'username': 'bigspuds777'}, {'id': 1596860505662144513, 'name': 'JOHN VIEIRA', 'username': 'JOHNVIE67080352'}, {'id': 1596860610905448449, 'name': 'biru nkumat', 'username': 'NkumatB'}, {'id': 1513497734711738374, 'name': 'elfiranda Hakim', 'username': 'Kidonk182'}, {'id': 1596836234860859392, 'name': 'Ecat Miao', 'username': 'sylvanasMa'}] as user                           MERGE (n:User {id: user.id, name: user.name, username: user.username })"
        ^}

我不知道为什么会发生这种情况,非常感谢任何帮助。

【问题讨论】:

    标签: python neo4j neo4j-python-driver


    【解决方案1】:

    您可能需要调整 Cypher 查询的语法以符合 Neo4j Cypher 查询语言规范。例如,MERGE 子句应使用 ON CREATE 和 ON MATCH 语法来指定在节点已存在或不存在时应采取的操作。

    以下是如何重写 Cypher 查询以使用 ON CREATE 和 ON MATCH 语法的示例:

    def test_batches(tx,user_batch):
        result = tx.run(f"UNWIND {user_batch} as user
        MERGE (n:User {{id: user.id, name: user.name, username: user.username }})
        ON CREATE SET n = user
        ON MATCH SET n += user")
    

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多