1

Is there a fancy or tricky way to apply an arbitrary array of jsonpatch-like to maps with cypher or even apoc?

So if i have

  • a map {a: 0, b: 0, c: 0}
  • a collection of operations encoded in maps, say [{replace: "a", value: 1}, {replace: "b", value: 1}]. I know it's not rellay jsonpatch but it's all I need. If there is a fully fledged jsonpatch solution, even better.

I seek a procedure, a concatenation of procedures or a neat way in cypher that produces {a: 1, b: 1, c: 0}

There is apoc.map.setKey(map, key, value) found in https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_map_functions but that only manipulates one value at once. I tried combining that with cypher's FOREACH but I need to indeed mutate a map and not a node.

my try was

create (:TEST {a: 0, b: 0, c: 0});

match (t:TEST)
with t, [{replace: "a", value: 1}, {replace: "b", value: 1}] as refs
foreach(ref in refs
    | set t = apoc.map.setKey(properties(t), ref.replace, 
ref.value))
return *;

Of course I could always let the client do the jsonpatch operations, but I prefer a way to do it within the tx. I know I could also write a procedure myself as a last resort.

tscherg
  • 1,032
  • 8
  • 22

1 Answers1

1

How about something like this...

MATCH (test:TEST {a:0, b:0, c:0})
SET test += {a: 1, b: 1}
RETURN test
Dave Bennett
  • 10,996
  • 3
  • 30
  • 41
  • thank you, I think that's much more concise than what I had. But still, I don't want to mutate a node, I just want to mutate a collection. – tscherg Jun 12 '18 at 15:03
  • sorry about that... i got stuck on "I seek a procedure, a concatenation of procedures or a neat way in cypher that produces {a: 1, b: 1, c: 0}" – Dave Bennett Jun 12 '18 at 15:19