Redis 通过字段值查找哈希 [英] Redis finding hashes by field values
问题描述
当使用 Redis 创建记录"时,您可以创建包含多个字段的哈希.例如:
When using Redis in order to create a "record" you can create a hash with several fields. For example:
HMSET myhash field1 "Hello" field2 "World"
HMSET myhash2 field1 "Goodbye" field2 "World"
您可以通过知道键值来检索它,但是我想知道有没有办法检索在 field2 中具有World"的所有哈希?
You can retrieve this by knowing the key values, however what I want to know is there any way to retrieve all hashes that have "World" in field2?
推荐答案
redis没有索引,没有实现SQL.这是一个键值存储.你提供一个键,它就会给你一个值.
There are no indexes in redis, and it doesn't implement SQL. It's a key-value store. You provide a key, it gets you a value.
也就是说,您可以通过自己维护二级索引来实现这一点.例如:
That said, you can implement this by maintaining secondary indexes yourself. For example:
创建记录和索引条目
HMSET myhash field1 Hello field2 World
SADD field2_world myhash
更新一条记录,删除旧的索引条目,创建一个新的
update a record, delete old index entry, create new one
SREM field2_world myhash
HMSET myhash field2 Mundo
SADD field2_mundo myhash
在field2
SMEMBERS field2_world
希望你能明白.
这篇关于Redis 通过字段值查找哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!