![](/img/trans.png)
[英]Py2neo (V4) - AttributeError: 'Graph' object has no attribute 'find_one'
[英]Which are the equivalent methods of py2neo-v2 merge_one, create_unique, find_one for py2neo v3?
我最近將我的 py2neo 庫從V2版本切換到 V3 版本,但我不知道執行某些操作的新命令。
特別是我堅持:
通過 label 和可選屬性匹配或創建節點並返回單個匹配節點。 此方法旨在與唯一約束一起使用,並且在找到多個匹配節點時不會失敗。 ( 閱讀文檔)
例如
from py2neo import Node, Graph
nicole = Node("Person", name="Nicole", age=24)
# adds the nicole element to the graph if it does not already exist a node labelled as "Person" having attribute "name" equal to "Nicole".
graph.merge_one("Person", "name", "Nicole") #<-- What's the equivalent py2neo V3 command?
在單個事務中創建一個或多個唯一路徑或關系。 這與 create() 類似,但使用 Cypher CREATE UNIQUE 子句來確保僅創建不存在的關系。 ( 閱讀文檔)
例如
from py2neo import Node, Relationship, Graph
kenny = Node("Person", name="Kenny")
graph.create(kenny)
kingfish = Node("Bar", name="Kingfish")
graph.create(kingfish)
rel = Relationship(kenny, "LIKES", kingfish)
# creates the relationship (kenny)-[:LIKES]->(kingfish), but only if it does not exist yet.
graph.create_unique(rel) #<-- What's the equivalent py2neo V3 command?
通過 label 和可選屬性查找單個節點。 此方法旨在與唯一約束一起使用,並且在找到多個匹配節點時不會失敗。 ( 閱讀文檔)
例如
from py2neo import Graph
# Find one node (and take only one if there are many) matching these conditions: its type is "Person" and it has an attribute "name" equal to "Kenny", and then save it inside "kenny" variable.
kenny = graph.find_one("Person", "name", "Kenny") #<-- What's the equivalent py2neo V3 command?
我發現這些方法在 py2neo V3 中不再可用,
那么這些方法對於 py2neo V3 的等價物是什么?
這些是 py2neo V3 的等效方法(也適用於py2neo V.2020.1 )
merge_one
-> merge
create_unique
-> 在你的例子中它只是merge
,因為這足以確保
僅創建不存在的關系。
find_one
->
這更復雜,必須分成更多的操作,
您可以使用NodeMatcher
function 和match
方法來做到這一點。
我將在下面的示例中直接展示它。
以下是將您的示例更改為 py2neo V3 命令:
通過 label 和可選屬性匹配或創建節點並返回單個匹配節點。 此方法旨在與唯一約束一起使用,並且在找到多個匹配節點時不會失敗。 ([閱讀文檔][3])
例如
from py2neo import Node, Graph
nicole = Node("Person", name="Nicole", age=24)
# adds the nicole element to the graph if it does not already exist a node labelled as "Person" having attribute "name" and "Person" and having their values equal to those of "nicole" element.
graph.merge(nicole, "Person", "name")
在單個事務中創建一個或多個唯一路徑或關系。 這與 create() 類似,但使用 Cypher CREATE UNIQUE 子句來確保僅創建不存在的關系。 ([閱讀文檔][4])
例如
from py2neo import Node, Relationship, Graph
kenny = Node("Person", name="Kenny")
graph.create(kenny)
kingfish = Node("Bar", name="Kingfish")
graph.create(kingfish)
rel = Relationship(kenny, "LIKES", kingfish)
# creates the relationship (kenny)-[:LIKES]->(kingfish), but only if it does not exist yet.
graph.merge(rel)
通過 label 和可選屬性查找單個節點。 此方法旨在與唯一約束一起使用,並且在找到多個匹配節點時不會失敗。 ([閱讀文檔][5])
例如
from py2neo import Graph
# Find one node (and take only one if there are many) matching these conditions: its type is "Person" and it has an attribute "name" equal to "Kenny", and then save it inside "kenny" variable.
from py2neo import NodeMatcher
matcher = NodeMatcher(graph)
kenny = matcher.match('Person', name='Kenny').first()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.