# TAG.SET

> Attach one or more tags to an existing key.

URL: https://docs.hotkv.com/commands/tag-set/

```
TAG.SET key tag [tag ...]
```

- Group: Tags
- Complexity: O(n), n being the tags
- HotKV Cloud: all plans
- On-Premise: no license needed

`TAG.SET` attaches labels to a key, so you can later find keys by label with
[TAG.MEMBERS](https://docs.hotkv.com/commands/tag-members/index.md) and [TAG.INTER](https://docs.hotkv.com/commands/tag-inter/index.md),
or filter vector searches with [VSIM.SEARCH](https://docs.hotkv.com/commands/vsim-search/index.md)
`FILTER`. A key can carry any number of tags, and a tag any number of keys.
Tags that the key already has are left as they are.

The key must exist in the selected database when you tag it. Tags work with
keys of any type.

## How tags behave

- Tags are kept per key name and shared by all logical databases:
  tagging `user:1` in database 0 also shows up for `user:1` in database 1.
- A key's tags go when the key does. That covers [DEL](https://docs.hotkv.com/commands/del/index.md),
  [UNLINK](https://docs.hotkv.com/commands/unlink/index.md), [GETDEL](https://docs.hotkv.com/commands/getdel/index.md), expiry,
  eviction, a command that empties the key (the last element popped from a
  list, the last member removed from a set, the last field removed from a
  hash), and [FLUSHDB](https://docs.hotkv.com/commands/flushdb/index.md) and
  [FLUSHALL](https://docs.hotkv.com/commands/flushall/index.md). A key created again under the same name
  starts with no tags. Overwriting a key that still exists, for example with
  [SET](https://docs.hotkv.com/commands/set/index.md), keeps its tags.
- [RENAME](https://docs.hotkv.com/commands/rename/index.md) and [RENAMENX](https://docs.hotkv.com/commands/renamenx/index.md) move the
  tags to the new name, and the tags of a key that a rename replaces are
  dropped. A `RENAMENX` that does nothing leaves the tags where they were.
  [MOVE](https://docs.hotkv.com/commands/move/index.md) keeps the tags, because tags are shared by all
  databases.
- Tags are not part of snapshots, so re-apply them after a server restarts
  from a snapshot.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Key exists | Integer: the number of tags newly attached | Integer |

Error replies: `ERR no such key` when the key does not exist.

## Examples

```
127.0.0.1:6379> SET article:1 "..."
OK
127.0.0.1:6379> TAG.SET article:1 news sports
(integer) 2
127.0.0.1:6379> TAG.SET article:1 sports europe
(integer) 1
127.0.0.1:6379> TAG.SET article:99 news
(error) ERR no such key
```

## Compatibility

HotKV-only command.

## See also

[TAG.REM](https://docs.hotkv.com/commands/tag-rem/index.md), [TAG.GET](https://docs.hotkv.com/commands/tag-get/index.md), [TAG.MEMBERS](https://docs.hotkv.com/commands/tag-members/index.md),
[TAG.INTER](https://docs.hotkv.com/commands/tag-inter/index.md), [VSIM.SEARCH](https://docs.hotkv.com/commands/vsim-search/index.md)