如何从py2neo获取自动节点ID? [英] How to get automatic node ID from py2neo?

查看:1027
本文介绍了如何从py2neo获取自动节点ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将py2neo 3.1.2版本与Neo4j 3.2.0结合使用,对此有疑问.在Neo4J的Web界面上,我可以运行以下查询来获取节点ID:

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)

MATCH (n:Person) RETURN ID(n)

我想知道py2neo API是否有做同样的事情.我已经检查过Node对象,但找不到任何东西.

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.

推荐答案

更新:上一个答案不适用于新的py2neo,但此答案有效

py2neo(4.0.0b12)的当前版本删除了remote方法.现在,您可以通过访问py2neo.data.Node.identity属性来获取NODE ID.这很简单.假设我使用py2neo这样查询我的neo4j数据库:

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

我们可以使用属性返回的节点ID通过查询数据库来确认NODE ID.如果它正常工作,则ab应该是同一节点.让我们做一个测试:

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

这篇关于如何从py2neo获取自动节点ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆