27

I have saved the data using semicolon.

redis> keys party:*

1) "party:congress:president"
2) "party:bjp:president"
3) "party:bjp"
4) "party:sena"

Is there any command that will list of all the parties? In case of above example, I expect

congress
bjp
sena
shantanuo
  • 31,689
  • 78
  • 245
  • 403

4 Answers4

36

No, there is no command to do that. But it would be trivial to implement it on client side, if you really have to.

Applications should never use the KEYS commands to retrieve data. KEYS blocks the whole Redis instance while it is scanning linearly the millions of keys you have stored. It is more a debugging command supposed to be used in administration tools.

With Redis, there is no btree structure to index the keys, so you cannot query for keys, except if your keys are stored in an existing collection (set, zset, etc ...)

Didier Spezia
  • 70,911
  • 12
  • 189
  • 154
  • I saw in [this answer](http://stackoverflow.com/a/30490869/1041046) that is possible to search by partial key. As I am just learning about redis, I'm not sure how accurate it is. I think you might need to update your answer to include more details. – AaA Oct 18 '16 at 06:19
  • 2
    It is possible to search by partial key, it is not possible to efficiently search by partial key, except if you explicitly create a data structure for this. Moreover, the original question here is not about searching, but about manipulation of the result (which is better done on client side). – Didier Spezia Oct 18 '16 at 08:04
  • Good point, I was looking for partial search in keys and this was second result in google. – AaA Oct 18 '16 at 09:59
23

You can use the SCAN command in redis to search for keys without blocking the whole database.

redis SCAN docs

This command has an optional MATCH filter that works much like the filter on the KEYS command.

redis> SCAN 0 MATCH party:*

1) <the cursor>
2) 1) "party:congress:president"
   2) "party:bjp:president"
   3) "party:bjp"
   4) "party:sena"

keep calling until the cursor returns back to 0 to get all parties (might not get ALL parties if they are being inserted while you are scanning)

available since 2.8

Peter Agnew
  • 361
  • 3
  • 3
15

You can use KEYS and * wildcard.

Example

SET user:1 Amir
SET user:2 Jack

To get all users using wildcard:

KEYS user:*

The result will be:

1) "user:1"
2) "user:2"
Amir Fo
  • 5,163
  • 1
  • 43
  • 51
  • 2
    Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets. – theo Feb 16 '22 at 09:29
1

I think if you want to get the 'parties' data from redis then each time you save your regular data you also save the party name to the parties list, then you can get it easily

sphism
  • 119
  • 1
  • 3