{
  "schema_version": 2,
  "id": "commands/zunionstore",
  "title": "ZUNIONSTORE",
  "url": "https://redis.io/docs/latest/commands/zunionstore/",
  "summary": "Stores the union of multiple sorted sets in a key.",
  "since": "2.0.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-18T09:48:43+01:00",
  "page_type": "content",
  "content_hash": "796fec303b74990b56dd1ae2bb0ed22be38e20f9740ad9f8295d8db3660373e0",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "> [!NOTE]\n> This command's behavior varies in clustered Redis environments. See the [multi-key operations](https://redis.io/docs/latest/develop/using-commands/multi-key-operations) page for more information.\n\n\nComputes the union of `numkeys` sorted sets given by the specified keys, and\nstores the result in `destination`.\nIt is mandatory to provide the number of input keys (`numkeys`) before passing\nthe input keys and the other (optional) arguments.\n\nBy default, the resulting score of an element is the sum of its scores in the\nsorted sets where it exists.\n\nUsing the `WEIGHTS` option, it is possible to specify a multiplication factor for\neach input sorted set. Each element's score is multiplied by its corresponding\nweight before aggregation.\nWhen `WEIGHTS` is not given, the multiplication factors default to `1`.\n\nWith the `AGGREGATE` option, it is possible to specify how the results of the\nunion are aggregated.\nThis option defaults to `SUM`, where the score of an element is summed across\nthe inputs where it exists.\nWhen this option is set to either `MIN` or `MAX`, the resulting set will contain\nthe minimum or maximum score of an element across the inputs where it exists.\nFor `SUM`, `MIN`, and `MAX`, each element's score is multiplied by its\ncorresponding weight before aggregation.\n\nWhen `AGGREGATE COUNT` is specified, the original element scores are ignored\nentirely. The resulting score for each element is determined by which input sets\ncontain it, optionally scaled by `WEIGHTS`:\n\n- Without `WEIGHTS`, each input set containing the element contributes `1` to\n  its score — effectively counting set membership.\n- With `WEIGHTS`, each input set containing the element contributes its\n  corresponding weight, so the score becomes the sum of those weights.\n\nThis enables a common use case: counting set membership frequency directly at\nthe command level, without application-side workarounds.\n\nIf `destination` already exists, it is overwritten."
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>destination</code></summary>\n\nThe key to store the resulting sorted set in.\n\n</details>\n\n<details open><summary><code>numkeys</code></summary>\n\nThe number of keys that follow.\n\n</details>\n\n<details open><summary><code>key [key ...]</code></summary>\n\nOne or more sorted-set keys to union.\n\n</details>"
    },
    {
      "id": "optional-arguments",
      "title": "Optional arguments",
      "role": "parameters",
      "text": "<details open><summary><code>WEIGHTS weight [weight ...]</code></summary>\n\nA multiplication factor for each input set; each set's scores are multiplied by its weight before aggregation. Defaults to 1 for every set.\n\n</details>\n\n<details open><summary><code>AGGREGATE SUM | MIN | MAX | COUNT</code></summary>\n\nHow to combine the scores of members that exist in multiple sets: `SUM` (the default), `MIN`, `MAX`, or `COUNT`.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "redis> ZADD zset1 1 \"one\"\n(integer) 1\nredis> ZADD zset1 2 \"two\"\n(integer) 1\nredis> ZADD zset2 1 \"one\"\n(integer) 1\nredis> ZADD zset2 2 \"two\"\n(integer) 1\nredis> ZADD zset2 3 \"three\"\n(integer) 1\nredis> ZUNIONSTORE out 2 zset1 zset2 WEIGHTS 2 3\n(integer) 3\nredis> ZRANGE out 0 -1 WITHSCORES\n1) \"one\"\n2) \"5\"\n3) \"three\"\n4) \"9\"\n5) \"two\"\n6) \"10\""
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the number of elements in the resulting sorted set.\n\n**RESP3:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the number of elements in the resulting sorted set."
    }
  ],
  "examples": []
}
