w3resource

Redis Sorted Sets: ZCOUNT

Redis ZCOUNT Command

Redis ZCOUNT command is used to return the number of elements in the sorted set at the key with a score between minimum and maximum.

Here are some common uses and scenarios for the "ZCOUNT" command:

  • Range Queries: Count members within a specified score range to retrieve relevant data.
  • Data Analysis: Perform analytical operations by counting elements that meet specific score criteria.
  • Segmentation: Segment data based on score ranges to facilitate further processing or analysis.
  • Statistical Calculations: Count elements within certain score bounds for statistical calculations.

Syntax:

ZCOUNT KEY_NAME

Available since

2.0.0.

Return Value

Integer replies the number of elements in the specified score range.

Return Value Type

Integer

Example: Redis ZCOUNT: Any member may belong to the set

127.0.0.1:6379> ZADD mycolorset  528 white  2514 black 850 red 128 pink 742 yellow
(integer) 5
127.0.0.1:6379> ZADD mycolorset  158 orange 1500 green 645 blue 426 gray
(integer) 4
127.0.0.1:6379> ZCOUNT mycolorset -inf +inf
(integer) 9

Example : Redis ZCOUNT : When the value is over 800 or more

127.0.0.1:6379> ZADD mycolorset  528 white  2514 black 850 red 128 pink 742 yellow
(integer) 5
127.0.0.1:6379> ZADD mycolorset  158 orange 1500 green 645 blue 426 gray
(integer) 4
127.0.0.1:6379> ZCOUNT mycolorset 800 +inf
(integer) 3

Example: Redis ZCOUNT: When the value among 100 and 500

127.0.0.1:6379> ZADD mycolorset  528 white  2514 black 850 red 128 pink 742 yellow
(integer) 5
127.0.0.1:6379> ZADD mycolorset  158 orange 1500 green 645 blue 426 gray
(integer) 4
127.0.0.1:6379> ZCOUNT mycolorset 100 500
(integer) 3

Example: Redis ZCOUNT: When the value among 300 and less

127.0.0.1:6379> ZADD mycolorset  528 white  2514 black 850 red 128 pink 742 yellow
(integer) 5
127.0.0.1:6379> ZADD mycolorset  158 orange 1500 green 645 blue 426 gray
(integer) 4
127.0.0.1:6379> ZCOUNT mycolorset -inf 300
(integer) 2

Previous: ZCARD
Next: ZINCRBY



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-zcount-key-min-max.php