0

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?

Nifim
  • 4,758
  • 2
  • 12
  • 31
Josh
  • 113
  • 1
  • 10
  • If you need help debugging code that you have written, you must post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and explain the specific problem with your code. – Skully Jan 08 '23 at 17:20

1 Answers1

0

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

koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15
  • I'm trying to create an array that is compatible with regular RedisJSON commands after creation. I'm looking at this blob and getting very confused as I am not familiar with Lua at all: https://github.com/RedisJSON/RedisJSON/blob/master/benchmarks/lua/json-set-path.lua – Josh Jan 09 '23 at 15:10
  • You mean how to construct a JSON string from Lua side? - Than try this in redis-cli: ```eval 'return(cjson.encode({KEY = {PATH1 = {COUNT = 5.0}}}))' 0``` Becomes: ```"{\"KEY\":{\"PATH1\":{\"COUNT\":5}}}"``` – koyaanisqatsi Jan 09 '23 at 18:59