如何解决如何在 py2neo (Neo4J & Python) 中返回节点 ID
目前正在使用 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也可以)
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?
非常感谢帮助
解决方法
这解决了问题。我希望这会在未来对某人有所帮助。
#retreive the ingredient_id of the last added ingredient in the Neo4j db
def last_ingredient_id():
ingredient = graph.run("MATCH (a:Ingredient) RETURN a.ing_id ORDER BY a.ing_id DESC").to_series()
result = int(ingredient[0])
return result
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。