Skip to main content

ZADD

Syntax

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

Module

sortedset

Categories

fast sortedset write

Description

Adds all the specified members with the specified scores to the sorted set at the key.

Options

  • NX - only adds the member if it currently does not exist in the sorted set.
  • XX - only updates the scores of members that exist in the sorted set.
  • GT - only updates the score if the new score is greater than the current score.
  • LT - only updates the score if the new score is less than the current score.
  • CH - modifies the result to return total number of members changed + added, instead of only new members added.
  • INCR - modifies the command to act like ZINCRBY, only one score/member pair can be specified in this mode.

Examples

Add elements to sorted set:

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
changedCount, err := vault.ZAdd(
"key",
map[string]float64{"member1": 2.5, "member2": 1.25, "member3": 3},
db.ZAddOptions{},
)

Add elements to sorted set, skipping members that already exist in the set:

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
changedCount, err := vault.ZAdd(
"key",
map[string]float64{"member1": 2.5, "member2": 1.25, "member3": 3, "member4": 4},
db.ZAddOptions{NX: true},
)

Increment the element by the specified score:

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
changedCount, err := vault.ZAdd(
"key",
map[string]float64{"member1": 5.75},
db.ZAddOptions{INCR: true},
)