Redis HMGET is used to retrieve the value of one or more fields stored in a hash. This is particularly useful when:
Let's assume we have a hash representing a user with fields like name, email, and age. Here's how you can use HMGET to fetch these fields.
const redis = require("redis"); const client = redis.createClient(); client.hmset("user:1001", "name", "John Doe", "email", "john@example.com", "age", "30", redis.print); client.hmget("user:1001", "name", "email", "age", function(err, result){ if (err) { console.error(err); } else { console.log(result); // Prints: [ 'John Doe', 'john@example.com', '30' ] } });
In this example, HMSET
is used to set the values and then HMGET
retrieves them. The result is an array that contains the values of the specified fields.
Q: What will happen if the requested field is not present in the hash?
A: Redis will return a special nil
value for fields that do not exist in the hash.
Q: Can I use HMGET to retrieve all fields and values from a hash?
A: No, to retrieve all fields and their respective values from a hash, use the HGETALL
command.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.