Skip to content

ZADD

ZADD key [NX | XX] [GT | LT] [CH] [INCR] score member [score member...]

ZADD adds all the specified members with the specified scores to the sorted set stored at key.

The score of the member should be an integer and two members can have the same score. Here are the options supported by the command:

  • NX: Only add new elements and do not update existing elements
  • XX: Only update existing elements and do not add new elements
  • GT: Only add new elements if the score is greater than the existing score
  • LT: Only add new elements if the score is less than the existing score
  • CH: Modify the return value from the number of new elements added to the total number of elements changed
  • INCR: When this option is specified, the scores provided are treated as increments to the score of the existing elements

The command by default returns the number of elements added to the sorted set.

localhost:7379> ZADD users 10 u1
OK 1
localhost:7379> ZADD users 5 u2
OK 1
localhost:7379> ZADD users 15 u3
OK 1
localhost:7379> ZADD users 12 u4
OK 1
localhost:7379> ZADD users 10 u1
OK 0
localhost:7379> ZADD users CH 11 u1
OK 1