我一直在試圖弄清楚如何將多個值放入一個鍵中,例如:{ "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
提交
取消