【问题标题】:How to select specific keys from Redis hash如何从 Redis 哈希中选择特定键
【发布时间】:2019-06-30 15:33:41
【问题描述】:

我创建了 Redis 哈希并存储在条目下方 - 101-104 是哈希键

HMSET 101  field1 101 field2 yy field3 bb field4 300
HMSET 102  field1 102 field2 xx field3 bb field4 300
HMSET 103  field1 103 field2 yy field3 bb field4 300
HMSET 104  field1 104 field2 xx field3 bb field4 300

我想检索所有具有 field2 = xx 的记录(这就像 SQL 中的where 子句)

我认为应该在 field2 上创建二级索引,但不知道如何编写。

【问题讨论】:

    标签: redis


    【解决方案1】:

    我认为您应该为您的键和值创建一个反向索引。即

     "value" ---> {{fieldName, HSETNAME}}
     Ex : xx --> {{field2,101},{field3,104}}
    

    遍历正确的键值,您可以获得 HMSET 名称,然后您可以从中检索所有记录。

    只是你必须根据你的用例来操作你的键和值。 你可以在这里找到更多信息:How do you search for keys with a value? For example get all KEYS where the value is "somevalue"

    【讨论】:

      【解决方案2】:

      Redis 不提供此类功能。

      此外,Redis 不允许创建任何索引。

      另外,它还提供各种数据结构供您根据访问需要保存数据,例如:列表、哈希图、集合、排序集等

      对于您的情况,您必须手动遍历所有记录才能获得所需的结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-19
        • 1970-01-01
        • 1970-01-01
        • 2018-05-29
        • 1970-01-01
        • 2013-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多