Currently working with Py2neo to access my Neo4J database.
I have trouble with returning a node id. I already went through the documentation of Py2neo and read multiple StackOverflow posts but none of them contains a solid answer to my question. I think more people could use this solution.
I'm able to locate the node using 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'})
How can I extract the Node ID (_6)?
The desired output would be
print(find_ingredient)
>>> 6
(_6 is also fine)
Second approach: I've added a property called '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')}]
The desired output would be
print(ing_id)
>>> 1
What code do I need to add to achieve this? Or is there an alternative (or better approach) to easily return the node id?
Help is much appreciated
This solved the problem. I hope this will help someone in the future.
#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