Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-01-2022 09:17 AM
I have my edge attributes in the form of dictionaries as we'd refer in python or maps. I was testing the working of updating one such edge attribute using APOC library. So, let's say I have an edge attribute qty_dict = {'5':1, '10':2}
I was trying to update this by merging it with another such map, suppose {'5':2, '4':1, '3':3}.
So, when I merge, I would like to see the resultant dict as qty_dict = {'5':3, '4':1, '3':3, '10':2}
Please have a look at what I've tried so far ...
Solved! Go to Solution.
12-01-2022 11:07 AM
I think you do not need to unwind map as ma and I think 'ma' on line 9 should be 'ma1' instead. As such, I think the following would be correct if it worked.
with apoc.convert.fromJsonMap("{'5':1,'10':2}") as map, apoc.convert.fromJsonMap("{'5':2,'4':1,'3':3}") as map1
unwind keys(map1) as ma1
call apoc.do.when(
apoc.map.get(map,ma1,'False') = 'False',
'return apoc.map.setKey(map,ma1,map1[ma1])',
'return apoc.map.setValues(map,[ma1,map1[ma1]+map[ma1]])',
{map:map, ma1:ma1, map1:map1}
)
yield value
return ma1, map1[ma1], value
It doesn't work because the value of map passed to the 'do.when' method on line 7 is always the original value of map, so it doesn't iteratively update the map.
You can try this approach. It seems to work:
with apoc.convert.fromJsonMap("{'5':1,'10':2}") as map, apoc.convert.fromJsonMap("{'5':2,'4':1,'3':3}") as map1
with [i in keys(map) where not i in keys(map1) | [i,map[i]]] as a,
[i in keys(map1) where not i in keys(map) | [i,map1[i]]] as b,
[i in keys(map) where i in keys(map1) | [i,map[i]+map1[i]]] as c
return apoc.map.fromPairs(a+b+c)
Result:
12-01-2022 11:07 AM
I think you do not need to unwind map as ma and I think 'ma' on line 9 should be 'ma1' instead. As such, I think the following would be correct if it worked.
with apoc.convert.fromJsonMap("{'5':1,'10':2}") as map, apoc.convert.fromJsonMap("{'5':2,'4':1,'3':3}") as map1
unwind keys(map1) as ma1
call apoc.do.when(
apoc.map.get(map,ma1,'False') = 'False',
'return apoc.map.setKey(map,ma1,map1[ma1])',
'return apoc.map.setValues(map,[ma1,map1[ma1]+map[ma1]])',
{map:map, ma1:ma1, map1:map1}
)
yield value
return ma1, map1[ma1], value
It doesn't work because the value of map passed to the 'do.when' method on line 7 is always the original value of map, so it doesn't iteratively update the map.
You can try this approach. It seems to work:
with apoc.convert.fromJsonMap("{'5':1,'10':2}") as map, apoc.convert.fromJsonMap("{'5':2,'4':1,'3':3}") as map1
with [i in keys(map) where not i in keys(map1) | [i,map[i]]] as a,
[i in keys(map1) where not i in keys(map) | [i,map1[i]]] as b,
[i in keys(map) where i in keys(map1) | [i,map[i]+map1[i]]] as c
return apoc.map.fromPairs(a+b+c)
Result:
12-01-2022 11:29 AM
Thank you, it worked in a jiffy! 🙂
All the sessions of the conference are now available online