来自 neo4j 和 python 的匹配计数

match count from neo4j and python

给出这个代码块

                parent_node_count = db.run(f"MATCH(parentNode) WHERE "
                              f"parentNode.LC = '{x[4]}' "
                              f"AND parentNode.U = '{x[5]}' "
                              f"AND parentNode.R = '{x[6]}' "
                              f"AND parentNode.Type = 'Parent' "
                              f"AND parentNode.Deck_Set = '{outline}' "
                              f" return parentNode")
            n = 0

            for record in parent_node_count:
                print(record["parent_node_count"]["id"])
                n = n + 1

            print("total number of records : " + str(n))
            total = int(n)

如何从 MATCH 查询中获取匹配项的数量?

在提供的代码中,它总是returns 0, 我在浏览器中检查过,匹配的计数不为零。

变量parent_node_count是一个neo4j.work.result.Result对象,它不包含对象id。您需要将其添加到密码查询中并通过 record.data() 对象访问它。请参见下面的示例:

    parent_node_count = tx.run(f"MATCH(parentNode) WHERE "
                          f"parentNode.LC = '{x[4]}' "
                          f"AND parentNode.U = '{x[5]}' "
                          f"AND parentNode.R = '{x[6]}' "
                          f"AND parentNode.Type = 'Parent' "
                          f"AND parentNode.Deck_Set = '{outline}' "
                          f" return parentNode, ID(parentNode) AS id")
    print(parent_node_count)
    n = 0

    for record in parent_node_count:
        print(record.data()["parentNode"])
        print(record.data()["id"])
        n = n + 1

    print("total number of records : " + str(n))
    total = int(n)


Result:
<neo4j.work.result.Result object at 0x7ff6cd035f60>    <- parent_node_count
{'Latitude': 28.5553263, 'Longitude': 68.2132128, 'Name': 'Dayna'} <- record
321  <- id
{'Latitude': 54.1863169, 'Longitude': 19.7204087, 'Name': 'Dione'} <- record
333  <- id
total number of records : 2