0
0
Redisquery~5 mins

TYPE command for key type check in Redis

Choose your learning style9 modes available
Introduction

The TYPE command helps you find out what kind of data is stored at a specific key in Redis. This is useful because Redis can store different types of data like strings, lists, or hashes.

When you want to check if a key holds a string, list, set, or other data type before working with it.
When debugging your Redis database to understand what data types your keys have.
When writing a program that needs to handle keys differently based on their data type.
When cleaning up or migrating data and you want to know the type of each key.
When you want to avoid errors by confirming the key type before performing operations.
Syntax
Redis
TYPE key

Replace key with the name of the key you want to check.

The command returns the type as a simple string like string, list, set, hash, zset, or none if the key does not exist.

Examples
Check the type of the key named mykey.
Redis
TYPE mykey
Find out what type of data is stored under the key user:1000.
Redis
TYPE user:1000
Check if shopping_list is a list, string, or another type.
Redis
TYPE shopping_list
Sample Program

This example sets a string key greeting, checks its type, adds an item to a list fruits, checks its type, and checks the type of a key that does not exist.

Redis
SET greeting "Hello"
TYPE greeting
LPUSH fruits "apple"
TYPE fruits
TYPE unknown_key
OutputSuccess
Important Notes

If the key does not exist, TYPE returns none.

Knowing the key type helps avoid errors when running commands that expect a certain data type.

Summary

TYPE command tells you the data type stored at a key.

It returns types like string, list, set, hash, zset, or none.

Use it to check key types before performing operations.