py2neo-v2 merge_one, create_unique, find_one for py2neo v3 的等效方法有哪些?
Which are the equivalent methods of py2neo-v2 merge_one, create_unique, find_one for py2neo v3?
我最近从 version V2 to version V3 切换了我的 py2neo 库,但我不知道执行某些操作的新命令。
特别是我坚持:
graph.merge_one
Match or create a node by label and optional property and return a
single matching node. This method is intended to be used with a unique
constraint and does not fail if more than one matching node is found. (read the docs)
例如
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?
graph.create_unique
Create one or more unique paths or relationships in a single
transaction. This is similar to create() but uses a Cypher CREATE
UNIQUE clause to ensure that only relationships that do not already
exist are created. (read the docs)
例如
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?
graph.find_one
Find a single node by label and optional property. This method is
intended to be used with a unique constraint and does not fail if more
than one matching node is found. (read the docs)
例如
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
,因为这
足以确保
only relationships that do not already exist are created.
find_one
->
这更复杂,必须拆分成更多的操作,
您可以使用 NodeMatcher
函数和 match
方法来完成。
我将在下面的示例中直接显示它。
以下是您更改为 py2neo V3 命令的示例:
graph.merge_one
Match or create a node by label and optional property and return a
single matching node. This method is intended to be used with a unique
constraint and does not fail if more than one matching node is found. ([read the docs][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")
graph.create_unique
Create one or more unique paths or relationships in a single
transaction. This is similar to create() but uses a Cypher CREATE
UNIQUE clause to ensure that only relationships that do not already
exist are created. ([read the docs][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)
graph.find_one
Find a single node by label and optional property. This method is
intended to be used with a unique constraint and does not fail if more
than one matching node is found. ([read the docs][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()
我最近从 version V2 to version V3 切换了我的 py2neo 库,但我不知道执行某些操作的新命令。
特别是我坚持:
graph.merge_one
Match or create a node by label and optional property and return a single matching node. This method is intended to be used with a unique constraint and does not fail if more than one matching node is found. (read the docs)
例如
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?
graph.create_unique
Create one or more unique paths or relationships in a single transaction. This is similar to create() but uses a Cypher CREATE UNIQUE clause to ensure that only relationships that do not already exist are created. (read the docs)
例如
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?
graph.find_one
Find a single node by label and optional property. This method is intended to be used with a unique constraint and does not fail if more than one matching node is found. (read the docs)
例如
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
,因为这 足以确保only relationships that do not already exist are created.
find_one
->这更复杂,必须拆分成更多的操作,
您可以使用NodeMatcher
函数和match
方法来完成。
我将在下面的示例中直接显示它。
以下是您更改为 py2neo V3 命令的示例:
graph.merge_one
Match or create a node by label and optional property and return a single matching node. This method is intended to be used with a unique constraint and does not fail if more than one matching node is found. ([read the docs][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")
graph.create_unique
Create one or more unique paths or relationships in a single transaction. This is similar to create() but uses a Cypher CREATE UNIQUE clause to ensure that only relationships that do not already exist are created. ([read the docs][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)
graph.find_one
Find a single node by label and optional property. This method is intended to be used with a unique constraint and does not fail if more than one matching node is found. ([read the docs][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()