Redis HEXISTS Command

In Redis, the HEXISTS command allows us to check whether a field exists in a given hash. We pass the name of the key and the field to the command, and it returns 1 if the field exists, and 0 if it doesn’t.

Syntax

The syntax goes like this:

HEXISTS key field

Example

Here’s an example of using HEXISTS to check for the existence of a field:

HEXISTS customer lastname

Result:

(integer) 1

This tells us that there is indeed a field called lastname in the hash stored at the customer key.

Let’s try another field:

HEXISTS customer height

Result:

(integer) 0

In this case, the height field doesn’t exist in the hash.

Non-Existent Keys

Running the command against a key that doesn’t exist results in 0 being returned:

HEXISTS nonexistentkey firstname

Result:

(integer) 0

Passing the Wrong Number of Arguments

Passing the wrong number of arguments results in an error:

HEXISTS customer

Result:

(error) ERR wrong number of arguments for 'hexists' command