2

I am using RedisJSON and I can't figure out how to get the full JSON values stored for multiple keys, following the documentation on the official site of the plugin.

For example, from the redis-cli:

redis-cli-1234> json.set k1 . '{"a":"a" }'
OK
redis-cli-1234> json.set k2 . '{"b":"b" }'
OK

Now I would like to run a command that would get k1, k2 (the keys of the desired values) and return their values, in one-go:

1) "{\"a\":\"a\"}"
2) "{\"b\":\"b\"}"

But I can't figure out how.

I've tried:

redis-cli-1234> json.mget k1 k2 .

And several other stuff, but I can only get redis to return either one value or the other, bot not both.

Any idea?

orcaman
  • 6,263
  • 8
  • 54
  • 69

1 Answers1

2

https://redis.io/topics/transactions

redis-cloud:6379> multi
OK
redis-cloud:6379(TX)> json.get k1
QUEUED
redis-cloud:6379(TX)> json.get k2
QUEUED
redis-cloud:6379(TX)> exec
1) "{\"a\":\"a\"}"
2) "{\"b\":\"b\"}"
Pieter Cailliau
  • 459
  • 3
  • 8
  • Thanks. This doesn't work for me for some reason, getting this error: redis-cloud-85> multi OK redis-cloud-85> json.get k1 QUEUED redis-cloud-85> json.get k2 (error) CROSSSLOT Keys in request don't hash to the same slot (context='within MULTI', command='json.get', original-slot='12706', wrong-slot='449', first-key='k1', violating-key='k2') – orcaman May 16 '21 at 06:36
  • 2
    @orcaman it seems like you're running with a Redis Cluster. Redis Transaction can't have cross slots operations. You can keys hash tags, see: https://redis.io/topics/cluster-spec#keys-hash-tags – Guy Korland May 16 '21 at 07:50
  • @GuyKorland thanks! Yup, running on Redis Labs :-). Is mget not really working for the RedisJSON plugin? Or at least not working as I have expected? I switched to working with standard SET and MGET for now (instead of the JSON.SET and JSON.MGET plugin functionality). – orcaman May 16 '21 at 08:38