0
0
Redisquery~5 mins

HEXISTS for field check in Redis

Choose your learning style9 modes available
Introduction

HEXISTS helps you quickly check if a specific field exists inside a hash in Redis. It tells you yes or no.

You want to see if a user's profile has a certain attribute before updating it.
You need to check if a product has a price field before calculating discounts.
You want to verify if a configuration setting exists before applying it.
You want to avoid overwriting data by checking if a field is already set.
You want to confirm if a session key has a particular property before processing.
Syntax
Redis
HEXISTS key field

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

field is the specific field inside the hash you want to verify.

Examples
Check if the field name exists in the hash user:1000.
Redis
HEXISTS user:1000 name
Check if the field price exists in the hash product:200.
Redis
HEXISTS product:200 price
Check if the field theme exists in the hash config:app.
Redis
HEXISTS config:app theme
Sample Program

First, we add fields name and age to the hash user:1. Then, we check if name exists (should be yes) and if email exists (should be no).

Redis
HSET user:1 name "Alice" age 30
HEXISTS user:1 name
HEXISTS user:1 email
OutputSuccess
Important Notes

HEXISTS returns 1 if the field exists, otherwise 0.

It only checks for the field inside the hash, not the key itself.

If the hash key does not exist, HEXISTS returns 0.

Summary

HEXISTS checks if a field exists inside a Redis hash.

It returns 1 for yes, 0 for no.

Use it to avoid errors or unwanted overwrites when working with hashes.