How do you search for keys with a value? For example get all KEYS where the value is “somevalue”

血红的双手。 提交于 2019-11-29 22:50:56

问题


   redis> SMEMBERS CO:1:A
    1) "1"
    2) "2"

   redis> SMEMBERS CO:2:A
    1) "1"
    2) "5"
    3) "6"

   redis> SMEMBERS CO:3:A
    1) "5"

   redis> SMEMBERS CO:4:A
    1) "1"

Now I want to find the keys where the value 1 exist

If I search with the values 1 then the result keys will be CO:1:A, CO:2:A and CO:4:A

How to get This?


回答1:


Redis is not a relational database. You need to anticipate this access path, and maintain a reverse index.

# This is your index
SADD CO:1:A 1 2
SADD CO:2:A 1 5 6
SADD CO:3:A 5
SADD CO:4:A 1

# Here is the reverse index
SADD REV:1 1 2 4
SADD REV:2 1
SADD REV:5 2 3
SADD REV:6 2

Now you can query in reverse way:

SMEMBERS REV:1
1) "1"
2) "2"
3) "4"
... meaning CO:1:A. CO:2:A, CO:4:A


来源:https://stackoverflow.com/questions/17806115/how-do-you-search-for-keys-with-a-value-for-example-get-all-keys-where-the-valu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!