HSTRLEN
The HSTRLEN
command in DiceDB is used to obtain the string length of value associated with field in the hash stored at a specified key.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The key of the hash, which consists of the field whose string length you want to obtain | String | Yes |
field | The field present in the hash whose length you want to obtain | String | Yes |
Return Value
Condition | Return Value |
---|---|
If specified key and field exists | string length |
If key doesn’t exist | 0 |
If field doesn’t exist | 0 |
Behaviour
- DiceDB checks if the specified key exists.
- If the key exists, is associated with a hash and specified field exists in the hash, DiceDB returns the string length of value associated with specified field in the hash.
- If the key does not exist, DiceDB returns
0
. - If the key exists and specified field does not exist in the key, DiceDB returns
0
.
Errors
-
Wrong type of key
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs when attempting to use the command on a key that contains a non-hash value.
- Error Message:
-
Wrong number of arguments
:- Error Message:
(error) -ERR wrong number of arguments for 'hstrlen' command
- Occurs if key or field isn’t specified in the command.
- Error Message:
Example Usage
Basic Usage
Creating hash myhash
with two fields field1
and field2
. Getting string length of value in field1
.
Invalid Usage on non-existent key
Getting string length from a non-existent key nonExistentHash
.
Invalid Usage on non-hash key
Getting string length from a key mystring
associated with a non-hash type.
Notes
- The
HSTRLEN
command has a constant-time operation, meaning its execution time is O(1), regardless of the number of fields in the hash.