I have created Redis hash and stored below entries - 101- 104 are the hash keys
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
I want to retrieve all the records having field2 = xx (this is like where clause from SQL)
I think it should be done my creating secondary index on field2 but not sure how to write it.
2 Answers 2
Redis doesn't provide such functionality.
Also, Redis doesn't allow any indexes to be created.
Alternatively, it provides various data structures for you to save data according to your access needs eg: list, hashmap, set, sorted sets, etc
For your case, you'll have to manually iterate through all the records to get the desired results.
Comments
I think you should create a reverse index for your keys and value. i.e.
"value" ---> {{fieldName, HSETNAME}}
Ex : xx --> {{field2,101},{field3,104}}
Iterating over proper key,value you can get the HMSET name and then you can retrieve all the records from it.
It is just that you have to manipulate your keys and values according to your usecase. You can find more info here: How do you search for keys with a value? For example get all KEYS where the value is "somevalue"