简体   繁体   English

如何使用py2neo删除Neo4j中的节点?

[英]How to delete a node in Neo4j using py2neo?

Using py2neo v4 to connect to my Neo4j database and I can't delete nodes via py2neo running a query that works fine in Cypher in the browser. 使用py2neo v4连接到我的Neo4j数据库,我无法通过py2neo删除运行运行在浏览器Cypher中正常运行的查询的节点。 Of course there is no real documentation for either Neo4j or py2neo, so hopefully I can get some help here. 当然,Neo4j或py2neo都没有真正的文档,因此希望我能在这里得到一些帮助。 There are similar questions, but both Neo4j and py2neo have new versions since then, and those questions/answers are either for other specific cases or are obsolete methods. 有类似的问题,但是从那时起Neo4j和py2neo都有新版本,这些问题/答案或者是针对其他特定情况,或者是过时的方法。

First, I define this function: 首先,我定义此函数:

def deleteNode(thisNodeID):
    graph.run("MATCH (n) where id(n) = $nodeID DETACH DELETE n", 
     parameters={"nodeID":thisNodeID})

Then I call the function like: 然后我像下面这样调用函数:

badObjectIDs = [268569,268535,268534]
for badID in badObjectIDs:
    deleteNode(badID)

This runs without any trouble, but doesn't delete anything and the nodes with those IDs are still in the database when I search via the browser. 这样可以毫无问题地运行,但是不会删除任何内容,并且当我通过浏览器搜索时,具有这些ID的节点仍在数据库中。

I also tried using py2neo's graph.delete() method, but again I wasn't able to get anything to work because there is no description or examples in the documentation to get it to work. 我也尝试过使用py2neo的graph.delete()方法,但是我又无法使任何事情起作用,因为文档中没有描述或示例来使它起作用。 I couldn't even find a way to get nodes by IDs in the documentation. 我什至找不到在文档中通过ID获取节点的方法。 Somthing like 有点像

graph.delete(matcher.match("Person"))

should delete all the nodes with the "Person" label, but instead it throws an error 应该删除所有带有“ Person”标签的节点,但是会引发错误

TypeError: No method defined to delete object <py2neo.matching.NodeMatch object at 0x0000026F52A8DC50>

So it's really just a basic question in using py2neo that should be explained clearly in the documentation or beginner tutorials, but again, there are no examples of using any of these methods anywhere I could find. 因此,实际上,这只是使用py2neo的一个基本问题,应该在文档或初学者教程中明确说明,但是同样,在任何可以找到的地方都没有使用任何这些方法的示例。

How do I remove nodes from my Neo4j database using py2neo? 如何使用py2neo从Neo4j数据库中删除节点?

I was able to delete a node, with ID=20 like this: 我能够删除ID = 20的节点,如下所示:

from py2neo import Graph, Node, Relationship

# Create graph
graph = Graph(host="localhost", auth=("neo4j", <insert_password>))

# Create nodes
nicole = Node('Person', name='Nicole')
adam = Node('Person', name='Adam')

# Create relationship between 2 nodes
graph.create(Relationship(nicole, 'KNOWS', adam))

# Select node with id = 20
id_20 = graph.evaluate("MATCH (n) where id(n) = 20 RETURN n")

# Delete node
graph.delete(id_20)

As for the function, it should work with something like this: 至于功能,它应该可以像下面这样工作:

def deleteNode(id):
    node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
    graph.delete(node)

You can yield the id of any node IN THE GRAPH by doing this: 您可以通过执行以下操作来产生图形中任何节点的ID:

node = graph.evaluate("MATCH (n) where id(n) = {} RETURN n".format(id))
node.identity

Just to be clear, I'm using neo4j-driver version 1.6.2 需要明确的是,我使用的是neo4j-driver版本1.6.2

You have to .commit() 您必须.commit()

tx = graph.begin()
matcher = NodeMatcher(graph)
node = matcher.get(node_id)
tx.delete(node)
tx.commit()

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

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