Search code examples
redisredisjson

How to get only specific properties for multiple keys with RedisJson MGET?


I would like to get only specific properties of multiple RedisJson objects stored in the DB.

For example, 2 RedisJson were created:

JSON.SET Cat:1 . '{"name":"Mitzi", "color":"pink", "age":1}'
JSON.SET Cat:2 . '{"name":"Lucky", "color":"black", "age":13}'

We are interested to get only properties "name" and "color" For a single object this would work:

JSON.GET Cat:1 .name .color

returns:

"{\".name\":\"Mitzi\",\".color\":\"pink\"}"

However, for multiple objects using MGET.

JSON.MGET Cat:1 Cat:2 .name .color

This obviously doesn't work, didn't figure out how to specify the properties as command arguments.


Solution

  • redis> JSON.MGET Cat:1 Cat:2 '$["name","color"]'
    ["Mitzi","pink"]
    ["Lucky","black"]