zadd

Adds elements to a sorted set.

If the key does not exist, it is created, holding an empty sorted set.

If the key already exists but does not hold a sorted set, an error is returned.

If a member to insert is already in the sorted set, its score is updated and the member is reinserted at the right position in the set.

[Redis documentation]

Arguments

Copied to clipboard!
zadd(key, elements, [options]);

Arguments Type Description
key
string
Sorted set key
elements
object[]
Elements to add
options
object
Optional query arguments

elements

The elements array lists the elements to add to the sorted set. Each element object has the following properties:

Properties Type Description
member
string
Member value
score
string
Member score

options

The options arguments can contain the following option properties:

Property Type (default) Description
ch
boolean (false)
If true, instead of returning the number of added elements, returns the number of changes performed
incr
boolean (false)
If true, instead of adding elements, increments the existing member with the provided score. Only one element can be specified if this option is set
nx
boolean (false)
If true, only adds new elements, without altering existing ones
queuable
boolean (true)
If true, queues the request during downtime, until connected to Kuzzle again
xx
boolean (false)
If true, ignores new elements, alters only existing ones

Resolve

Resolves to the number of added elements or, if the ch option is set, resolves to the number of changes performed.

Usage

Copied to clipboard!
try {
  await kuzzle.ms.zadd('ssetfoo', [
    {member: 'foo', score: '42'},
    {member: 'bar', score: '4'},
    {member: 'baz', score: '-272.15'}
  ]);

  // Prints:
  // [ { member: 'baz', score: -272.15 },
  //   { member: 'bar', score: 4 },
  //   { member: 'foo', score: 42 } ]
  console.log(await kuzzle.ms.zrange('ssetfoo', 0, -1));
} catch (error) {
  console.error(error.message);
}