Redis KEYS – How to get one or more keys stored in redis datastore

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to get one or more keys stored in redis datastore by using a COMMAND – KEYS in redis-cli.

This command is used to return one or more keys that matches a specified pattern. The pattern are specified in glob-style.

Glob Style Patterns :-

  1. * wildcard :- It matches zero or more of any characters including spaces, for example foo* matches to fooccc, foo, fooq.
  2. ? wildcard :- It matches exactly one of any characters including spaces, for example f?0 matches to fao, fbo, fco.
  3. [list] wildcard :- It matches exactly one character from the list, for example f[abc]0 matches to fao, fbo, fco but not fdo, feo.
  4. [^list] wildcard :- It matches exactly one character which is not in the list, for example f[^abc]0 matches to fdo, feo but not fao, fbo.
  5. [a-z] wildcard :- It matches exactly one character from a to z, similarly [A-Z], [0-9] matches one character from A to Z and 0 to 9 respectively.

Use \ to escape special characters.

The syntax of redis KEYS command is as follows :-

Syntax :-

redis host:post> KEYS <pattern>

Output :- 

- (array) reply, representing the list of keys matching the given pattern.

Example :-

Redis KEYS

References :-

  1. KEYS Command Docs

That’s all for how to get one or more keys stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.