If you get an error that reads “WRONGTYPE Operation against a key holding the wrong kind of value” when using the ZMPOP
or BZMPOP
commands in Redis, it’s because you’re passing a key with the wrong data type.
To fix this issue, make sure you pass a sorted set to these commands.
Example of Error
Here’s an example of code that causes the error:
ZMPOP 1 countries MIN
Result:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
In my case, the countries
key holds a set (not a sorted set), which is why I got the error.
We can use the TYPE
command to check a key’s type:
TYPE countries
Result:
set
As suspected, the key holds a set, which is the wrong data type for the ZMPOP
command (and the BZMPOP
command).
Solution
The solution is to make sure the keys we pass to ZMPOP
(and BZMPOP
) hold a sorted set.
Let’s replace the countries
key with another key that holds a sorted set:
ZMPOP 1 cats MIN
Result:
1) "cats" 2) 1) 1) "Meow" 2) "1"
This time we didn’t get the error. That’s because the cats
key holds a sorted set.
Let’s check:
TYPE cats
Result:
zset
It’s a sorted set, as expected.