0
0
Redisquery~5 mins

HLEN for field count in Redis

Choose your learning style9 modes available
Introduction

HLEN helps you find out how many fields are inside a hash. It tells you the size of the hash.

You want to know how many details are stored about a user in a profile hash.
You need to check if a hash has any data before processing it.
You want to count the number of settings saved in a configuration hash.
You want to monitor how many items are stored in a hash for debugging.
You want to limit the number of fields in a hash and check if it is reached.
Syntax
Redis
HLEN key

key is the name of the hash you want to check.

The command returns an integer showing the number of fields in the hash.

Examples
Returns the number of fields in the hash named user:1000.
Redis
HLEN user:1000
Returns how many settings are stored in the config:app hash.
Redis
HLEN config:app
Sample Program

This example creates a hash user:1 with three fields: name, age, and city. Then it uses HLEN to count how many fields are in that hash.

Redis
HMSET user:1 name "Alice" age "30" city "Paris"
HLEN user:1
OutputSuccess
Important Notes

If the key does not exist, HLEN returns 0.

Only fields directly inside the hash are counted, not nested data.

Summary

HLEN counts the number of fields in a hash.

It helps check the size or presence of data in a hash.

Returns 0 if the hash does not exist.