Redis ZMSCORE Command Explained

In Redis, the ZMSCORE command allows us to get the score of one or members in a sorted set.

Syntax

The syntax goes like this:

ZMSCORE key member [member ...]

Example

Suppose we create the following sorted set:

ZADD cats 15 Meow 27 Fluffy 43 Scratch 84 Purr 25 Bite

We can now use ZMSCORE to return the score of one or more members of that sorted set:

ZMSCORE cats Fluffy Purr

Result:

1) "27"
2) "84"

Here I returned the scores from two members in the cats sorted set.

When a Member Doesn’t Exist

When a member doesn’t exist, the score for that member is nil:

ZMSCORE cats Fluffy Homer Purr

Result:

1) "27"
2) (nil)
3) "84"

Wrong Data Type

When the key contains the wrong data type, an error is returned:

ZMSCORE animals Fluffy Purr

Result:

(error) WRONGTYPE Operation against a key holding the wrong kind of value

In this case, the animals key contains a set (not a sorted set), and so we got an error.

We can use the TYPE command to check the key’s data type:

TYPE animals

Result:

set

As suspected, it’s a set.