{"id":28366,"date":"2023-04-01T11:34:30","date_gmt":"2023-04-01T01:34:30","guid":{"rendered":"https:\/\/database.guide\/?p=28366"},"modified":"2023-04-01T11:34:31","modified_gmt":"2023-04-01T01:34:31","slug":"redis-zlexcount-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-zlexcount-command-explained\/","title":{"rendered":"Redis ZLEXCOUNT Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>ZLEXCOUNT<\/code> command returns the number of elements in a sorted set with a value between two given values. It can be used when we force lexicographical ordering by applying the same score to all members of a sorted set.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZLEXCOUNT key min max<\/code><\/pre>\n\n\n\n<p>The <em><code>min<\/code><\/em> and <em><code>max<\/code><\/em> arguments must start with either <code>(<\/code> or <code>[<\/code> to indicate whether the range is inclusive or exclusive. When it starts with the <code>(<\/code> character, it&#8217;s exclusive, when it starts with the <code>[<\/code> character it&#8217;s inclusive. This is demonstrated in the following examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we create the following sorted set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g<\/code><\/pre>\n\n\n\n<p>Let&#8217;s use the <code>ZLEXCOUNT<\/code> command to find out how many elements in the sorted set have a value between two values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZLEXCOUNT myzset &#91;b &#91;f<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 5<\/pre>\n\n\n\n<p>In this case I used the <code>[<\/code> argument to make it inclusive.<\/p>\n\n\n\n<p>The integer reply of <code>5<\/code> tells us that there are five members with values between our range of <code>b<\/code> and <code>f<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Inclusive vs Exclusive<\/h2>\n\n\n\n<p>In the previous example we started the <em><code>min<\/code><\/em> and <em><code>max<\/code><\/em> values with the <code>[<\/code> character. This indicates that it&#8217;s an inclusive range. In other words, the <code>b<\/code> and the <code>f<\/code> were included when determining the result.<\/p>\n\n\n\n<p>We can alternatively use the <code>(<\/code> character to specify an exclusive range: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZLEXCOUNT myzset (b (f<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 3<\/pre>\n\n\n\n<p>In this case, the <code>b<\/code> and <code>f<\/code> were not included in the range (i.e. the range went from <code>c<\/code> to <code>e<\/code>). <\/p>\n\n\n\n<p>We can make one argument inclusive while the other is exclusive:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZLEXCOUNT myzset &#91;b (f<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 4<\/pre>\n\n\n\n<p>In that case the <code>b<\/code> was included in the calculation but the <code>f<\/code> wasn&#8217;t.<\/p>\n\n\n\n<p>Here it is the other way around:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZLEXCOUNT myzset (b &#91;f<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 4<\/pre>\n\n\n\n<p>Same result, but this time the <code>b<\/code> was excluded, while the <code>f<\/code> was included.<\/p>\n\n\n\n<p>We can verify this by using the <code><a href=\"https:\/\/database.guide\/redis-zrange-command-explained\/\" data-type=\"post\" data-id=\"28144\">ZRANGE<\/a><\/code> command to return the actual members:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZRANGE myzset (b &#91;f BYLEX<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1) \"c\"\n2) \"d\"\n3) \"e\"\n4) \"f\"<\/pre>\n\n\n\n<p>As expected, <code>b<\/code> was excluded, while the <code>f<\/code> was included.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Include All Members<\/h2>\n\n\n\n<p>We can use the special values of&nbsp;<code>+<\/code>&nbsp;or&nbsp;<code>-<\/code>&nbsp;to specify all members in the sorted set (assuming they all have the same score). These characters indicate the positively infinite and negatively infinite strings respectively.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZLEXCOUNT myzset - +<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 7<\/pre>\n\n\n\n<p>If you find that it returns zero, but you think that&#8217;s wrong, check that you&#8217;ve got them around the right way:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZLEXCOUNT myzset + -<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 0<\/pre>\n\n\n\n<p>Here, I used <code>+ -<\/code> instead of <code>+ -<\/code> and so no elements were included.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the ZLEXCOUNT command returns the number of elements in a sorted set with a value between two given values. It can be used when we force lexicographical ordering by applying the same score to all members of a sorted set.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,20],"class_list":["post-28366","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=28366"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28366\/revisions"}],"predecessor-version":[{"id":29995,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28366\/revisions\/29995"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=28366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=28366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=28366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}