Redis MGET Command Explained

In Redis, the MGET command allows us to get the values of multiple keys at once. The values are returned in an array reply.

If a specified key doesn’t exist, MGET returns nil for that key.

Syntax

The syntax goes like this:

MGET key [key ...]

Which basically means we can pass as many keys as we want.

Example

Let’s first set multiple keys using MSET:

MSET name "Woof" type "Dog" age "10"

Result:

OK

Now let’s use MGET to return those values:

MGET name type age

Result:

1) "Woof"
2) "Dog"
3) "10"

When a Key Doesn’t Exist

If a specified key doesn’t exist, MGET returns nil for that key:

MGET name type age height

Result:

1) "Woof"
2) "Dog"
3) "10"
4) (nil)