我一直在试图弄清楚如何将多个值放入一个键中,例如:{ "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
添加回答
举报
0/150
提交
取消