13

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"

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?

asked Jun 26, 2012 at 14:27

2 Answers 2

28

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:

create a record and an index entry

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

find all records which have "World" in field2

SMEMBERS field2_world

I hope you get the idea.

answered Jun 26, 2012 at 14:36
Sign up to request clarification or add additional context in comments.

2 Comments

i executed the exact commands in the answer with 2 hashes and it did not work. may you explain a bit more?
@SHM: How it didn't work? Show your entire command history for that session. With outputs.
-2

For next readers, the SORT command would probably help using the "BY pattern" argument

HMSET myhash field1 Hello field2 World
SORT myhash BY World

Patterns examples (w3resource)

answered Jan 12, 2021 at 16:09

2 Comments

Doesn't work on Redis 7.0.0, failing with error (error) WRONGTYPE Operation against a key holding the wrong kind of value
Also this is sorting a specific key. The original question was how to get that specific key, that you are assuming one already knows.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.