如何使用 RedisJson 的 MGET 方法仅获取多个键的特定属性?

huangapple go评论51阅读模式
英文:

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

问题

我想要从数据库中获取多个 RedisJson 对象的特定属性。

例如,创建了2个 RedisJson 对象:

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


我们只对获取属性 "name" 和 "color" 感兴趣。
对于单个对象,可以这样工作:

JSON.GET Cat:1 .name .color

返回:

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

然而,对于多个对象,使用 MGET。

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

这显然不起作用,还没有找到如何指定属性作为命令参数。
英文:

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.

答案1

得分: 3

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

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

huangapple
  • 本文由 发表于 2023年4月4日 15:09:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定