Redis Hash Commands

Author: Al-mamun Sarkar Date: 2020-09-18 09:23:45

Redis hash commands are used to manage the HashMap data of Redis storage. In this session, I will show you how to store HashMap/Dictionary, how to retrieve and to delete HashMap data form the Redis database. 

 

Store HashMap into Redis:

Syntax Set multiple fields and values:

HMSET key field2 value2 field2 value2

Example:

HMSET student fname "Jone" lname "Doe" age 27 roll 10

 

Getting Data of HashMap:

Syntax:

HGETALL key

Example:

HGETALL student

Get the value of the given fields:

Syntax:

HMGET key field1 field2 ...

Example:

HMGET student fname lname

Set single field value of a hash:

Syntax:

HSET key field value

Example:

HSET student reg 2423

 Delete one or multiple hash fields:

Syntax:

HDEL key field

Example:

HDEL student1 fname lname

Check a field exists in the hash or not:

Syntax:

HEXISTS key field

Example:

HEXISTS student roll

 Get the value of the hash field:

Syntax:

HGET key field

Example:

GET student roll

Increment integer field value by the given number:

Syntax:

HINCRBY key field increment

Example:

HINCRBY student roll 2

Get all keys of a hash:

Syntax:

HKEYS key

Example:

HKEYS student

Get all values of a hash:

Syntax:

HVALS key

Example:

HVALS student

Get the number of fields of hash:

Syntax:

HLEN key

Example:

HLEN studen

Set a single field value of hash if the field doesn't exist:

Syntax:

HSETNX key field value

Example:

HSETNX student phone 01923234553454

 

All of these commands in tabular format:

Commands Description Syntax Example
Set a single field value of a hash HSET key field value HSET student reg 2423
Delete one or multiple hash fields HDEL key field HDEL student1 fname lname
Check a field exists in the hash or not HEXISTS key field HEXISTS student roll
Get the value of the hash field HGET key field HGET student roll
Increment integer field value by the given number HINCRBY key field increment HINCRBY student roll 2
Get all keys of a hash HKEYS key HKEYS student
Get all values of a hash HVALS key HVALS student
Get the number of fields of hash HLEN key HLEN student
Set a single field value of hash if field doesn't exist HSETNX key field value HSETNX student phone 01923234553454