EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Redis Tutorial Redis HMGET
 

Redis HMGET

Updated February 15, 2023

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

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

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

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - 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
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

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

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*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?

🚀 Limited Time Offer! - 🎁 ENROLL NOW