I am having a json array of user detail nodes and I do loop over it and check if a node already exists with the specific numeric id property; if it exists then check the 'id' property of the node, if the 'id' is numeric( isdigit ) then do nothing but if 'id' is not numeric property then update the ' id ' property and set it to the ' numeric_id '.
for x in all_json:
f_id = x.get("id",'')
f_num_id = x.get('numaric_id','')
tnode = self.find_user_friend(f_num_id, uid)
if tnode is not None:
#### Update id to numeric id #####
if tnode['n']['id'].isdigit():
pass
else:
tnode['n']['id'] = f_num_id
tnode['n'].push()
find_user_friend function is
def find_user_friend(self, t_id, u_id):
"""
To search for target with respective user
"""
tnode=None
query = """
Match (n:Friend {type:"1"}) where n.numaric_id={tid} and n.uid = {uid} return n
"""
result = self.graph.run(query, parameters={'tid':str(t_id), 'uid':int(u_id)})
if result.forward() > 0:
tnode = result.current()
The current process takes time as it searches for every individual node then checks and then update the node. I am looking for the optimized solution. I am beginner in neo4j and familiar with UNWIND and FOREACH in neo4j but can't really made it for now.
Here is what I tried.
list_arr = [{id:"amey",numaric_id:"123"},{id:"12",numaric_id:"12"},{id:"amiey",numaric_id:"113"}]
UNWIND """+ l +""" as c
Match (n:Friend {numaric_id : c.numaric_id , uid : '1', type:"1"})
SET n.id = numaric_id
Help will be appreciated