ZADD
Syntax
ZADD key [NX | XX] [GT | LT] [CH] [INCR] score member [score member...]
Module
sortedsetCategories
fast sortedset writeDescription
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
- Go (Embedded)
- CLI
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},
)
Add elements to sorted set:
> ZADD key 2.5 member1 1.25 member2 3 member3
Add elements to sorted set, skipping members that already exist in the set:
> ZADD key NX 2.5 member1 1.25 member2 3 member3 4 member4
Increment the element by the specified score:
> ZADD key INCR 5.75 member1