Redis Lua that will create a default JSON on numincrby exception?

77 views Asked by At

I am trying to run numincrby on a nested JSON that might or might not exist via multiple threads. If the key does not exist, a default JSON structure has to be created with the value 0 and the value incremented.

e.g. JSON.NUMINCRBY "KEY" ".PATH1.COUNT" 5.0 on {'KEY': {'PATH1': {...}}} or {'KEY': {...}} should result in {'KEY': {'PATH1': {'COUNT' : 5.0}}}

Unfortunately the default behaviour of RedisJSON is throw an error.

How can I achieve this behaviour with a Lua?

1

There are 1 answers

2
koyaanisqatsi On

In Lua you can make decitions by condition at re/defining too, e.g.

function flipflop(b)
local b = b == false and true or false
return(b)
end

At work...

> chk = true
> chk
true
> chk = flipflop(chk)
> chk
false
> chk = flipflop(chk)
> chk
true

EDIT: Impression to my Comment
enter image description here