Py2neo - How to use merge_one function along with several attributes for my node?

I overcame the problem of avoiding the creation of duplicate nodes in my database using the merge_one functions, which work like this:

t=graph.merge_one("User","ID","someID") 

which creates a node with a unique identifier. My problem is that I cannot find a way to add multiple attributes / properties to my node along with an identifier that is added automatically (e.g. date). I managed to achieve this old "duplicated" method, but now it does not work, since merge_one cannot take more arguments! Any ideas ???

+2
source share
3 answers

Graph.merge_one "-", node. - node id merge_one, ?

t = graph.merge_one("User", "ID", "someID")
t['name'] = 'Nicole'
t['age'] = 23
t.push()
+5

, ... , ,

py2neo == 2.0.7 ( Node.properties):

... PropertySet, dict.

, :

m = graph.merge_one("Model", "mid", MID_SR)
m.properties.update({
    'vendor':"XX",
    'model':"XYZ",
    'software':"OS",
    'modelVersion':"",
    'hardware':"",
    'softwareVesion':"12.06"
})

graph.push(m)
+1

, , . ( ) , .

def find_multiProp(graph, *labels, **properties):
    results = None
    for l in labels:
        for k,v in properties.iteritems():
            if results == None:
                genNodes = lambda l,k,v: graph.find(l, property_key=k, property_value=v)
                results = [r for r in genNodes(l,k,v)]
                continue
            prevResults = results
            results = [n for n in genNodes(l,k,v) if n in prevResults]
    return results

( ) node, ...

def merge_one_multiProp(graph, *labels, **properties):
    r = find_multiProp(graph, *labels, **properties)
    if not r:
        # remove tuple association
        node,= graph.create(Node(*labels, **properties))
    else:
        node = r[0]
    return node

...

from py2neo import Node, Graph
graph = Graph()

properties = {'p1':'v1', 'p2':'v2'}
labels = ('label1', 'label2')

graph.create(Node(*labels, **properties))
for l in labels:
    graph.create(Node(l, **properties))
graph.create(Node(*labels, p1='v1'))

node = merge_one_multiProp(graph, *labels, **properties)
0

All Articles