【发布时间】:2021-04-22 09:08:27
【问题描述】:
目前正在使用 Py2neo 访问我的 Neo4J 数据库。
我无法返回节点 ID。我已经阅读了 Py2neo 的文档并阅读了多篇 StackOverflow 帖子,但没有一篇包含对我的问题的可靠答案。我认为更多的人可以使用这个解决方案。
我可以使用 NodeMatcher 定位节点
from py2neo import Graph, NodeMatcher
from py2neo import Node, Relationship
graph = Graph("bolt://localhost:7687")
matcher = NodeMatcher(graph)
find_ingredient = matcher.match("Ingredient", name="Onion").first()
print(find_ingredient)
>>> (_6:Ingredient {name: 'Onion'})
如何提取节点 ID (_6)?
期望的输出是
print(find_ingredient)
>>> 6
(_6也可以)
第二种方法:我添加了一个名为“ing_id”的属性
ingredient = graph.run("MATCH (n:Ingredient {name:'Ui'}) WHERE n.name='Ui' RETURN n")
data = ingredient.data()
print(data)
>>>[{'n': Node('Ingredient', ing_id=1, name='Ui')}]
期望的输出是
print(ing_id)
>>> 1
我需要添加什么代码来实现这一点? 或者是否有替代(或更好的方法)可以轻松返回节点 ID?
非常感谢您的帮助
【问题讨论】: