使用 Python 在 neo4j 中将列表作为参数传递
Pass list as argument in neo4j using Python
我正在尝试 运行 这个查询,其中参数是一个列表:
codes = ["123","234"]
query = graph.run("""
MATCH (n:NAME)
where n.codes = "{}"
RETURN return n.commonName as commonName""".format(codes))
我正在尝试将“代码”作为参数传递给 Neo4j 查询。这会引发错误“列表在查询中不可读”
如有任何建议,我们将不胜感激。
您可以使用我在测试中使用的以下代码:
from py2neo import Graph
session = Graph("http://localhost:7474", auth=("neo4j", "*****"))
//create a dictionary of key:value
param = {"codes":["Angy","Rodney"]}
//use $<dict_key> in your query. For ex: $codes
query = """
MATCH (n:Customer)
where n.Name in $codes
RETURN n"""
//Run the result
result = session.run(query, parameters=param).data()
//Below are my debugging/print to check what is inside the node
print ("What is data type of result? ", type(result))
print ("What is the data type of each item? ", type(result[0]))
print ("What are the keys of the dictionary? ", result[0].keys())
print ("What is the class of the node? ", type(result[0].get('n')))
print ("How to access the first node? ", result[0].get('n'))
print ("How to access values inside the node? ", result[0].get('n',{}).get('Name'))
Result:
What is data type of result? <class 'list'>
What is the data type of each item? <class 'dict'>
What are the keys of the dictionary? dict_keys(['n'])
What is the class of the node? <class 'py2neo.data.Node'>
How to access the first node? (_327:Customer {Latitude: 48.509075, Longitude: -2.7383235, Name: 'Angy'})
How to access values inside the node? Angy
我正在尝试 运行 这个查询,其中参数是一个列表:
codes = ["123","234"]
query = graph.run("""
MATCH (n:NAME)
where n.codes = "{}"
RETURN return n.commonName as commonName""".format(codes))
我正在尝试将“代码”作为参数传递给 Neo4j 查询。这会引发错误“列表在查询中不可读”
如有任何建议,我们将不胜感激。
您可以使用我在测试中使用的以下代码:
from py2neo import Graph
session = Graph("http://localhost:7474", auth=("neo4j", "*****"))
//create a dictionary of key:value
param = {"codes":["Angy","Rodney"]}
//use $<dict_key> in your query. For ex: $codes
query = """
MATCH (n:Customer)
where n.Name in $codes
RETURN n"""
//Run the result
result = session.run(query, parameters=param).data()
//Below are my debugging/print to check what is inside the node
print ("What is data type of result? ", type(result))
print ("What is the data type of each item? ", type(result[0]))
print ("What are the keys of the dictionary? ", result[0].keys())
print ("What is the class of the node? ", type(result[0].get('n')))
print ("How to access the first node? ", result[0].get('n'))
print ("How to access values inside the node? ", result[0].get('n',{}).get('Name'))
Result:
What is data type of result? <class 'list'>
What is the data type of each item? <class 'dict'>
What are the keys of the dictionary? dict_keys(['n'])
What is the class of the node? <class 'py2neo.data.Node'>
How to access the first node? (_327:Customer {Latitude: 48.509075, Longitude: -2.7383235, Name: 'Angy'})
How to access values inside the node? Angy