How to Use TYPE Command in Redis: Syntax and Examples
Use the
TYPE command in Redis to find out the data type stored at a specific key. The command returns types like string, list, set, hash, zset, or none if the key does not exist.Syntax
The TYPE command syntax is simple. You provide the key name as an argument, and Redis returns the type of value stored at that key.
- TYPE key: Returns the data type of the key.
Possible return values include string, list, set, hash, zset (sorted set), or none if the key does not exist.
redis
TYPE key
Example
This example shows how to use the TYPE command to check the data type of different keys in Redis.
redis
SET mystring "hello" LPUSH mylist "world" SADD myset "a" HSET myhash field1 "value1" TYPE mystring TYPE mylist TYPE myset TYPE myhash TYPE nonexistentkey
Output
string
list
set
hash
none
Common Pitfalls
One common mistake is assuming the TYPE command returns the value stored at the key instead of its type. Another is checking a key that does not exist, which returns none. Also, remember that TYPE only tells you the top-level data type, not the contents.
Wrong usage example:
TYPE mystring GET mystring
Correct usage is to use TYPE to check type, then use the appropriate command to get the value.
redis
TYPE mystring GET mystring
Output
string
"hello"
Quick Reference
| Data Type | Description |
|---|---|
| string | Simple string value |
| list | Ordered list of strings |
| set | Unordered collection of unique strings |
| hash | Key-value pairs within a key |
| zset | Sorted set with scores |
| none | Key does not exist |
Key Takeaways
Use
TYPE key to find the data type stored at a Redis key.The command returns types like string, list, set, hash, zset, or none if the key is missing.
Check the type before using commands specific to that data type to avoid errors.
If
TYPE returns none, the key does not exist in the database.Remember
TYPE shows the data type, not the actual value stored.