简体   繁体   English

如何从py2neo获取自动节点ID?

[英]How to get automatic node ID from py2neo?

I'm using py2neo 3.1.2 version with Neo4j 3.2.0 and I have a question about it. 我正在将py2neo 3.1.2版本与Neo4j 3.2.0结合使用,对此有疑问。 At Neo4J's web interface I can run the following query to get the nodes ids: 在Neo4J的Web界面上,我可以运行以下查询以获取节点ID:

MATCH (n:Person) RETURN ID(n)

I'd like to know if there's something at py2neo API that does that same thing. 我想知道py2neo API是否有做同样的事情。 I've already inspected the Node object, but I couldn't find anything about it. 我已经检查了Node对象,但找不到任何相关信息。

I've talked with @technige at Twitter (py2neo's creator) and his answer was. 我在Twitter(py2neo的创建者)上与@technige进行了交谈,他的回答是。

Ah right. 对啊 It's a bit indirect but you can do: 这有点间接,但是您可以执行以下操作:

from py2neo import remote remote(node)._id

Update: Previous Answer does not work with new py2neo but this answer works 更新:以前的答案不适用于新的py2neo但此答案有效

The current version of py2neo (4.0.0b12) dropped the remote method. 当前版本的py2neo (4.0.0b12)删除了remote方法。 Now you can get the NODE ID by accessing the py2neo.data.Node.identity attribute. 现在,您可以通过访问py2neo.data.Node.identity属性获取NODE ID It's quite simple. 这很简单。 Let's say I query my neo4j database using py2neo like this: 假设我使用py2neo这样查询我的neo4j数据库:

#########################
# 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. 我们可以使用属性返回的节点ID通过查询数据库来确认NODE ID。 If it worked correctly, a and b should be the same node. 如果工作正常,则ab应该是同一节点。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM