[英]Proper collection in Redis that will allow updating and deleting by field
I want to store a collection of weather forecasts as JSON objects in redis:我想将一组天气预报存储为 redis 中的 JSON 对象:
{
"city":"London",
"weather":"SUNNY",
"lastPrediction": "..",
"source":"..."
}
Each item has a unique city name and there would be up to tens of thousand weather forecasts.每个项目都有一个唯一的城市名称,并且会有多达数万个天气预报。
I want to be able to add those to the collection, find and update a single item by the city and remove it by the city as well.我希望能够将它们添加到集合中,按城市查找和更新单个项目,并按城市将其删除。
I wonder which structure should I use - I especially do not know how to update and delete entries by a certain field.我想知道我应该使用哪种结构 - 我特别不知道如何更新和删除某个字段的条目。
Since each item has an unique "city", hash works fine for your case.由于每个项目都有一个独特的“城市”,因此 hash 非常适合您的情况。
HSET
again, and delete your fields with HDELHSET
更新您的字段,并使用HDEL删除您的字段 here is the demo;这是演示;
127.0.0.1:6379> HSET london weather SUNNY lastPrediction "foo" source "foobar"
(integer) 3
127.0.0.1:6379> HGET london weather
"SUNNY"
127.0.0.1:6379> HGETALL london
1) "weather"
2) "SUNNY"
3) "lastPrediction"
4) "foo"
5) "source"
6) "foobar"
127.0.0.1:6379> HSET london weather CLOUDY
(integer) 0
127.0.0.1:6379> HGET london weather
"CLOUDY"
127.0.0.1:6379> HDEL london source
(integer) 1
127.0.0.1:6379> HGETALL london
1) "weather"
2) "CLOUDY"
3) "lastPrediction"
4) "foo"
127.0.0.1:6379>
Please consider redis keys and fields are case sensitive.请考虑 redis 键和字段区分大小写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.