# Cypher Query for updating a list of nodes

**URL:** <https://community.neo4j.com/t/cypher-query-for-updating-a-list-of-nodes/27533>\
**Category:** Cypher\
**Tags:** apoc, cypher\
**Created:** [October 20, 2020, 12:32pm UTC](https://community.neo4j.com/t/cypher-query-for-updating-a-list-of-nodes/27533 "2020-10-20T12:32:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alishahaan9](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/alishahaan9/32/4376_2.png) [@alishahaan9](https://community.neo4j.com/u/alishahaan9)\
**Post date:** [October 20, 2020, 12:32pm UTC](https://community.neo4j.com/t/cypher-query-for-updating-a-list-of-nodes/27533/1 "2020-10-20T12:32:28Z")

</div>

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**'.

```auto
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

```auto
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"}]

```auto
            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

---

<div class="post-metadata">

**Author:** ![david\_allen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/david_allen/32/13090_2.png) [@david\_allen](https://community.neo4j.com/u/david_allen)\
**Post date:** [October 20, 2020, 12:50pm UTC](https://community.neo4j.com/t/cypher-query-for-updating-a-list-of-nodes/27533/2 "2020-10-20T12:50:02Z")

</div>

UNWIND is the way to go, but the way you're passing your array to the query isn't going to work. This:

```auto
UNWIND """+ l +""" as c

```

Is trying to unwind a string!

The thing to do is to submit your python array as a parameter, and then unwind the parameter. Something like this:

```auto
query = """
UNWIND $inputArray AS c
Match (n:Friend {numaric_id : c.numaric_id , uid : '1', type:"1"})
            SET n.id = numaric_id
"""
self.graph.run(query, parameters={'inputArray': l})

```

---

<div class="post-metadata">

**Author:** ![alishahaan9](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/alishahaan9/32/4376_2.png) [@alishahaan9](https://community.neo4j.com/u/alishahaan9)\
**Post date:** [October 20, 2020, 1:07pm UTC](https://community.neo4j.com/t/cypher-query-for-updating-a-list-of-nodes/27533/3 "2020-10-20T13:07:56Z")

</div>

Hi david,  
Thanks for responding. What if we unwind a string then how can it be handled?

Like this:

l = '[{id:"amey",numaric\_id:"123"},{id:"12",numaric\_id:"12"},{id:"amiey",numaric\_id:"113"}]'

---

<div class="post-metadata">

**Author:** ![david\_allen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/david_allen/32/13090_2.png) [@david\_allen](https://community.neo4j.com/u/david_allen)\
**Post date:** [October 20, 2020, 1:14pm UTC](https://community.neo4j.com/t/cypher-query-for-updating-a-list-of-nodes/27533/4 "2020-10-20T13:14:32Z")

</div>

You simply can't unwind a string; unwind only works on lists/arrays. I dunno -- I guess it might work, if it did, a string would unwind to individual characters?

Given your array you want to unwind it to individual objects with id/numaric\_id keys and process each of these sequentially.

Doing something like this """ + l + """ interpolates the python string representation of the array into an actual string, which is not the same thing.

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [October 23, 2020, 12:10am UTC](https://community.neo4j.com/t/cypher-query-for-updating-a-list-of-nodes/27533/5 "2020-10-23T00:10:03Z")

</div>

Looks like this is a JSON string. You would need to first convert this into a structure (a list of maps), then you could UNWIND the list so you have a map on each row.

You can use APOC convert functions for this:

```auto
WITH '[{id:"amey",numaric_id:"123"},{id:"12",numaric_id:"12"},{id:"amiey",numaric_id:"113"}]' as inputString
WITH apoc.convert.fromJsonList(inputString) as list
UNWIND list as row
RETURN row.numaric_id

```

If the JSON string is the input, then just use apoc.convert.fromJsonList() on the parameter.

---

<div class="post-metadata">

**Author:** ![david\_allen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/david_allen/32/13090_2.png) [@david\_allen](https://community.neo4j.com/u/david_allen)\
**Post date:** [October 23, 2020, 10:46am UTC](https://community.neo4j.com/t/cypher-query-for-updating-a-list-of-nodes/27533/6 "2020-10-23T10:46:44Z")

</div>

it's not JSON as the keys are lacking quotations around them. It's a python dictionary, which is why I'm thinking submitting it raw as a parameter is the way to go
