Redis finds hashes by field values

When using Redis to create a β€œrecord”, you can create a hash with multiple fields. For instance:

HMSET myhash field1 "Hello" field2 "World" HMSET myhash2 field1 "Goodbye" field2 "World" 

You can get this by knowing the values ​​of the key, however I want to know if there is a way to get all the hashes that have "World" in field2?

+4
source share
1 answer

Redis has no indexes, and it does not implement SQL. This is a keystore. You provide the key, it gives you value.

However, you can implement this by storing secondary indexes yourself. For instance:

create record and index record

 HMSET myhash field1 Hello field2 World SADD field2_world myhash 

update record, delete old index record, create new

 SREM field2_world myhash HMSET myhash field2 Mundo SADD field2_mundo myhash 

find all records having "World" in field2

 SMEMBERS field2_world 

Hope you have an idea.

+10
source

All Articles