EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis HMGET
Secondary Sidebar
Redis Tutorial
  • Redis
    • Redis Pubsub
    • Redis GUI
    • Redis Database
    • Redis Get All Keys
    • Redis TTL
    • Redis EXPIRE
    • Redis Version
    • Redis EXPIRE
    • Redis List
    • Redis CLI Commands
    • Redis HSET
    • Redis FLUSHDB
    • Redis Key
    • Redis Persistence
    • Redis FLUSHALL
    • Redis pipeline
    • Redis SADD
    • Redis ZADD
    • Redis ZSET
    • Redis Get
    • Redis Replication
    • Redis Delete All Keys
    • Redis Use Cases
    • Redis HMSET
    • Redis Transactions
    • Redis Monitor
    • Redis HGET
    • Redis SETEX
    • Redis Rails
    • RedisJSON
    • Redis Get Key
    • Redis Auth
    • Redis gem
    • Redis Key Count
    • RedisTimeSeries
    • Redis Timeout
    • Redis LPUSH
    • Redis Architecture
    • Redis API
    • Redis Message Queue
    • Redis RPUSH
    • Redis Protocol
    • Redis Data Types
    • Redis Namespace
    • Redis ZRANGE
    • Redis Master Slave
    • Redis Memory Usage
    • Redis WATCH
    • Redis URL
    • Redis Key Value
    • Redis INCR
    • Redis EXISTS
    • Redis INFO
    • Redis HMGET
    • Redis Operator
    • Redis NOAUTH Authentication Required
    • Redis LRANGE
    • Redis RDB
    • Redis TLS
    • Redis HDEL
    • Redis WebSocket
    • Redis HGETALL
    • Redis GETSET
    • Node-RED WebSocket

Redis HMGET

Introduction to Redis HMGET

Redis HMGET is associated with fields that were stored in the key. If a field does not exist in the hash value of redis, it will return nil. The redis HMGET command returns the value associated with the field. Non-existent keys in it will be treated as empty hashes and will run against the redis HMGET command.

Redis HMGET

Key Takeaways

  • We are using HMGET to get the value associated with the specified field. By using the field we are retrieving the specified values.
  • We can use single as well as multiple fields at the time of using the HMGET command on a specified key name.

What is Redis HMGET?

When we use the HMGET command, we use multiple fields with the key that we defined. If we do not define any fields with the hset or set commands at the same time, the HMGET command will return nil in output. If we define field1 as val1 and field2 as val2, and we use three fields in the HMGET command, it will return val1, val2, and nil in the output.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The HMGET command is used to retrieve values for multiple fields defined in the hset command. As we know, a hash is a primitive data type in Redis that represents the mapping between a field and the associated value. Redis hashes are more compact and were designed to take up less space for key-value pairs.

Syntax:

Below is the syntax of the HMGET command. Redis HMGET command contains the key name and field name parameter. We can use multiple field names in a single command by using the HMGET command.

HMGET name_of_key field1 field2 …. fieldN

In the above syntax, we can see that we are using a single key but using multiple fields to display the output using the HMGET command.

The parameter description of the HMGET command is as follows:

  • HMGET – This command is used to display the value associated with specified key fields. We are using the HMGET command in single as well as multiple fields.
  • Name of Key – This is the key name that we are creating using the hset command. This is a very important parameter while using the HMGET command. We need to define the key name while using the HMGET command.
  • Field1 to fieldN – This parameter is used to display the value of the specified field. At the time of using a field with a specified key by using the redis HMGET command, it will return the specified field value in the output.

Redis HMGET Key Field

To define the key and field in the HMGET command we need to create the key and field value with the hset command. In the below example, we are creating the first key name as key_red and defining the field as field1 and value as val1. We also create the second key as key_red1 and define the field as field2 and value as val2 as follows.

Command:

HSET key_red field1 "val1"
HSET key_red1 field2 "val2"

Output:

Redis HMGET - Key and Field

After creating the key and field we are using the HMGET command to display the value of the specified field. In the below example, we can see that we are using a single field with and specified key as follows. We are using the key name as key_red and using the field name as field1 as follows.

Command:

HMGET key_red field1

Output:

value of specified field

Now in the below example, we can see that we are adding another field to the key_red keys. We are adding the field name as field2 and defining the value as val2 as follows.

Command:

HSET key_red field2 "val2"

Output:

Redis HMGET- Adding a value

After adding the new key field and value, we are using both fields with the specified keys by using the hmget command. In the below example, we can see that while using multiple fields it will return all the outputs of the specified key field as follows.

Command:

HMGET key_red field1 field2

Output:

Output of specified key field

Redis HMGET Command

HMGET command is used to display the specified value from the specified key field. For using the HMGET command first we need to create the redis hash, we are creating the redis hash by using the hset command. In the below example, we are creating the key name as student.

Command:

hset student stud_id 1 stud_name "ABC" stud_addr "Pune"

Output:

Redis HMGET - Key name

In the above example we can see that we have created a key name as student and defined the field name as stud_id, stud_name, and stud_addr also we have defined the value as 1, ABC, and Pune. In the below example, we are using the single field to define the redis HMGET command as follows. We are using the field name as stud_id.

Command:

HMGET student stud_id

Output:

Redis HMGET5 - single field

In the below example, we are using two fields to define the redis HMGET command as follows. We are using field names as stud_id and stud_name.

Command:

HMGET student stud_id stud_name

Output:

Redis HMGET - two fields

In the below example, we are using three fields to define the redis HMGET command as follows. We are using field names as stud_id and stud_name and stud_addr.

Command:

HMGET student stud_id stud_name stud_addr

Output:

Redis HMGET - three fields

Redis HMGET Value

As we know that redis HMGET returns the list of values associated with the fields which we have given. The field values are displayed in the same order in which they were received. In the below example we are creating the key name as emp and defining the field name as emp_id, emp_name, and emp_addr also we are defining the value as 101, ABC, and Mumbai as follows.

Command:

hset emp emp_id 101 emp_name "ABC" emp_addr "Mumbai"

Output:

Redis HMGET - field values

After creating the key and field, we can see in the following example that we are providing the field in the order in which they appear and in the output that it will appear in the order from which it will come as follows.

Command:

HMGET emp emp_id emp_name emp_addr

Output:

field in the order

In the example below, we can see that we are not providing the field in sequence, so it will appear in the sequence from which we are providing it as follows.

Command:

HMGET emp emp_addr emp_name emp_id

Output:

not providing the field in sequence

FAQs

Given below are the FAQs mentioned:

Q1. What is the use of redis HMGET command?

Answer: We are using the HMGET command in redis to retrieve the value of the specified field which we are using.

Q2. How many fields we are using with HMGET command in redis?

Answer: We are using single or multiple fields at the time of using the redis HMGET command. We need to use the key name with a specified field.

Q3. Which command we are using in redis for creating the redis HMGET key and field?

Answer: We are using the Hset command for creating the key with field and value in redis.

Conclusion

Redis HMGET command basically used to get the values for multiple fields which we have defined in the Hset command. It is associated with fields that were stored in the key. If field does not exist in the hash value of redis then it will return the nil value.

Recommended Articles

This is a guide to Redis HMGET. Here we discuss the introduction, redis HMGET key field, command, value, and FAQs respectively. You may also have a look at the following articles to learn more –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more