HEXISTS is used to determine if a hash field exists or not. It is common to use it when there's a need to avoid storing duplicate values in a hash or checking the existence of a key before performing any operations.
Let's assume we have a Redis client instance
Example 1: Checking if a field exists in the hash:
In this example, we're setting a field
name with value
John for the hash
user:1001. We then use
hexists to check if the field
name exists in the hash. It returns true because the field exists.
Example 2: Checking a non-existent field in the hash:
In this example, we're checking if the
age field exists in the hash
user:1001. It doesn't, so
hexists returns false.
HEXISTScan be used to check for duplicates, it's better to use sets if your main operation is to prevent duplicate entries as sets are designed for that purpose.
HEXISTSonly checks if a field exists in a hash, not the key itself. If you use it on a non-hash key type, it will return an error.
HEXISTSreturns 1 (true) if the field exists and 0 (false) if it does not exist.
HEXISTSto check if a key exists? No,
HEXISTSis designed to check if a field exists within a hash. Use
EXISTSto check if a key exists.