簡體   English   中英

如何在 py2neo (Neo4J & Python) 中返回節點 ID

[英]How to return the node ID in py2neo (Neo4J & Python)

目前正在使用 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)?

所需的 output 將是

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')}]

所需的 output 將是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM