Selective hash reads
Read a selected subset of attributes from a hash without fetching unrelated fields.
Redis · Module
Returns the values of multiple fields from a Redis hash, preserving field order and representing missing fields with nil.
HMGET reads several fields from the hash stored at key in one command. Redis returns one result for each requested field, preserving request order and using a nil value when a field is absent.
HMGET key field [field ...]| Name | Type | Description |
|---|---|---|
key | key | The name of the key that holds the hash. |
field [field ...] | field names | One or more fields whose values to retrieve. |
An array containing one value for each requested field, in request order; an absent field yields nil.
redis-cli HSET user:42 name Ada role engineer
redis-cli HMGET user:42 name role missingThe result contains values aligned with the requested fields; the missing field produces a nil entry.
Read a selected subset of attributes from a hash without fetching unrelated fields.
Fetch several values in one command to reduce client-server round trips.
HMGET retrieves multiple fields from one Redis hash using a single command. Its time complexity is O(N), where N is the number of requested fields.
Follow-up: How does it differ from HGETALL? HGETALL returns every field and value, while HMGET limits the response to the explicitly requested fields.
The returned values follow the same order as the fields in the request. This lets callers associate each result with its corresponding input field by position.
Follow-up: What happens for an absent field? Redis places a nil value at that field's corresponding result position.
The key must identify a hash, and the command requires at least one field name. Asking for a field that is not present returns nil rather than failing the entire command.
Follow-up: What if the key contains another Redis data type? The command reports a wrong-type error because HMGET operates on hashes.
HGETHGETALLHSET