Rishab
Rishab

Reputation: 31

How to update multiple values of json with where condition in redis?

I have a JSON stored in redis. I wanted to update two of its value using a where condition for same key. I am able to update for a single attribute, but how to update two attributes?

JSON.SET TDSS "$.accounts[?(@.Code == 'abc')].feeAdj[0].feeAmount" "\"1000\""

This is working fine for single value updates. How can I update two values in JSON with condition of Code == 'abc'?

I tried JSON.MSET, it is not working properly.

Upvotes: 3

Views: 297

Answers (1)

Lior Kogan
Lior Kogan

Reputation: 20618

Yes, you can do that:

redis> JSON.SET x $ '[{"code":"abc", "val1":1, "val2":2}, {"code":"def", "val1":5, "val2":6}]'

Let's start with retrieving the values of two elements:

redis> JSON.GET x '$[?(@.code=="abc")]["val1", "val2"]'
"[1,2]"

Now, let's change the values of these two elements:

redis> JSON.MERGE x '$[?(@.code=="abc")]' '{"val1":3, "val2":4}'
OK

And retrieve the values of these two elements again:

redis> JSON.GET x '$[?(@.code=="abc")]["val1", "val2"]'
"[3,4]"

Note that JSON.MERGE is available since Redis Stack 7.2.

Upvotes: 2

Related Questions