How to get automatic node ID from py2neo?

你离开我真会死。 提交于 2019-12-12 21:06:00

问题


I'm using py2neo 3.1.2 version with Neo4j 3.2.0 and I have a question about it. At Neo4J's web interface I can run the following query to get the nodes ids:

MATCH (n:Person) RETURN ID(n)

I'd like to know if there's something at py2neo API that does that same thing. I've already inspected the Node object, but I couldn't find anything about it.


回答1:


Update: Previous Answer does not work with new py2neo but this answer works

The current version of py2neo (4.0.0b12) dropped the remote method. Now you can get the NODE ID by accessing the py2neo.data.Node.identity attribute. It's quite simple. Let's say I query my neo4j database using py2neo like this:

#########################
# Standard Library Imports
#########################

import getpass

#########################
# Third party imports
#########################

import py2neo

# connect to the graph
graph = py2neo.Graph(password=getpass.getpass())

# enter your cypher query to return your node
a = graph.evaluate("MATCH (n:Person) RETURN n LIMIT 1")

# access the identity attribute of the b object to get NODE ID
node_id = a.identity

We can confirm the NODE ID by querying our database using the node id returned by the attribute. If it worked correctly, a and b should be the same node. Let's do a test:

# run a query but use previous identity attribute
b = graph.evaluate("MATCH (n) WHERE ID(n)={} RETURN n".format(node_id))

# test for equality; if we are right, this evaluates to True
print(a == b)
[Out]: True



回答2:


I've talked with @technige at Twitter (py2neo's creator) and his answer was.

Ah right. It's a bit indirect but you can do:

from py2neo import remote remote(node)._id



来源:https://stackoverflow.com/questions/44117334/how-to-get-automatic-node-id-from-py2neo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!