Redis ZADD command is used to add one or more members with their scores to a sorted set. The syntax of the command is:
ZADD key [NX|XX] [CH] [INCR] score member [score member …]
- key: The name of the sorted set.
- NX: Optional keyword that only adds new members and ignores the ones that already exist in the set.
- XX: Optional keyword that only updates the scores of existing members and ignores the ones that do not exist in the set.
- CH: Optional keyword that returns the number of changed members.
- INCR: Optional keyword that increments the score of a member by the given value if it already exists in the set, or adds it with the given score if it does not exist.
- score: The score associated with the member, which determines its position in the sorted set.
- member: The member to add to the set.
Example usage:
ZADD myset 1 “one” ZADD myset 2 “two” ZADD myset 3 “three” 4 “four” ZADD myset NX 5 “five” XX 3 “three”
The above commands create a sorted set named “myset” and add members with their respective scores. The fourth command uses both NX and XX options to selectively add new and update existing members.