Redis provides some easy ways to check whether or not a field exists in a given hash. Here are five commands that we can use to check for the existence of a field in a given hash.
The HEXISTS
Command
Redis provides the HEXISTS
command specifically to enable us to check for the existence of a field. All we do is pass the name of the key that contains the hash, and the name of the field that we’re checking for.
Example:
HEXISTS pet age
Result:
(integer) 1
In this example, I checked the pet
key for a field called age
. I got an integer reply of 1
, which means that the field does exist.
Here’s what happens when the field doesn’t exist:
HEXISTS pet weight
Result:
(integer) 0
We get a zero when the field doesn’t exist. Same if the key doesn’t exist.
According to the Redis documentation, the time complexity for this command is: O(1)
The HGET
Command
The HEXISTS
command outlined above is probably all we need to check for the existence of a field. But that’s not to say that other commands can’t provide us with our desired information.
For example, the HGET
command returns the value of the given field, and nil
if the field doesn’t exist:
HGET pet age
Result:
"11"
In this case, the field exists, and we get its value.
Here’s what happens when the field doesn’t exist:
HGET pet weight
Result:
(nil)
We’ll also get the same result if the key doesn’t exist.
According to the Redis documentation, the time complexity for this command is: O(1)
The HMGET
Command
The HMGET
command allows us to return the value of multiple fields at once. As with the HGET
command, it returns nil
for any fields that don’t exist:
HMGET pet name color age weight
Result:
1) "Bark" 2) (nil) 3) "11" 4) (nil)
In this case, two fields exist and two don’t. For the ones that exist, we get their values. For the fields that don’t exist, we get nil
.
Be mindful of the performance implications of using commands like this. According to the Redis documentation, the time complexity of this command is: O(N) where N is the number of fields being requested.
The HKEYS
Command
The HKEYS
command returns the names of all fields in the hash stored at the given key. In other words, when we run this command, all field names are returned for the specified hash. This means we can immediately see if a given field name is in the hash or not. If the field name we’re interested in is not included in the result, then it doesn’t exist in the hash.
Example:
HKEYS pet
Result:
1) "name" 2) "age" 3) "height"
So we can see that the hash contains three fields, and we can see each of their names.
According to the Redis documentation, the time complexity of this command is: O(N) where N is the size of the hash.
The HGETALL
Command
The HGETALL
command returns all fields and values from the hash. If the HKEYS
command seems like overkill, the HGETALL
command is probably way over the top. Either way, it still allows us to check for the existence of a key while returning all fields and their values.
Example:
HGETALL pet
Result:
1) "name" 2) "Bark" 3) "age" 4) "11" 5) "height" 6) "9.11999999999999922"
According to the Redis documentation, the time complexity of this command is: O(N) where N is the size of the hash.