为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 Redis 将多个值添加到一个键中

如何使用 Redis 将多个值添加到一个键中

红糖糍粑 2023-06-27 17:30:05
我一直在试图弄清楚如何将多个值放入一个键中,例如:{    "fruit": {        "tomato": {            "Color": "red",            "Price": "100"        },        "banana": {            "Color": "yellow",            "Price": "150"        }    }}现在我当前的代码是:r = serialized_redis.MsgpackSerializedRedis(host='localhost', port=6379, db=0)r.set("fruit", {"tomato": {"Color": "red", "Price": "100"}})r.set("fruit", {"banana": {"Color": "yellow", "Price": "150"}})问题是,每次我执行 r.set 时,它似乎都会替换,这意味着当我运行此代码时,它只会被设置为:{    "fruit": {        "banana": {            "Color": "yellow",            "Price": "150"        }    }}所以即使我做了一组“番茄”,它也会被“香蕉”取代,因为它是正在设置的最新一个。我的问题是,如何将它添加到同一个键但具有不同的值,以便所有内容都在同一个键中但具有不同的“furits”?{    "fruit": {        "tomato": {            "Color": "red",            "Price": "100"        },        "banana": {            "Color": "yellow",            "Price": "150"        }    }}
查看完整描述

1 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

你可以使用哈希值

hmset fruit tomato your_json_serialized_here
hmset fruit orange ...

hmset你也可以像这样用多种水果来做hmset fruit apple 1 banana 2 orange 3

使用 hmget 从哈希中访问数据就像这样hmget fruit orange banana apple


查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 81 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信