How to Return Multiple Keys at Once in Redis

In Redis, the GET command is typically used to return the value of a single key that holds a string. But what if we need the values from multiple keys?

We can use the MGET command.

Example

Suppose we set the following keys:

MSET firstname "Rob" lastname "Banks" age 35

Result:

OK

We can now use MGET to return the value of all of those keys:

MGET firstname lastname age

Result:

1) "Rob"
2) "Banks"
3) "35"

The result is an array reply that contains a list of values at the specified keys.

If a key doesn’t hold a string, or if it doesn’t exist at all, then nil is returned for that key:

MGET firstname nickname scores

Result:

1) "Rob"
2) (nil)
3) (nil)