You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Added sorted set example. (#2005)
* Added sorted set example. * Update sorted-set.js Co-authored-by: Leibale Eidelman <leibale1998@gmail.com>
This commit is contained in:
@@ -15,6 +15,7 @@ This folder contains example scripts showing how to use Node Redis in different
|
|||||||
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
|
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
|
||||||
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
|
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
|
||||||
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
|
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
|
||||||
|
| `sorted-set.js` | Add members with scores to a Sorted Set and retrieve them using the ZSCAN iteractor functionality |
|
||||||
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command |
|
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command |
|
||||||
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command |
|
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command |
|
||||||
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io) |
|
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io) |
|
||||||
|
35
examples/sorted-set.js
Normal file
35
examples/sorted-set.js
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
// Add several values with their scores to a Sorted Set,
|
||||||
|
// then retrieve them all using ZSCAN.
|
||||||
|
|
||||||
|
import { createClient } from 'redis';
|
||||||
|
|
||||||
|
async function addToSortedSet() {
|
||||||
|
const client = createClient();
|
||||||
|
await client.connect();
|
||||||
|
|
||||||
|
await client.zAdd('mysortedset', [
|
||||||
|
{
|
||||||
|
score: 99,
|
||||||
|
value: 'Ninety Nine'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
score: 100,
|
||||||
|
value: 'One Hundred'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
score: 101,
|
||||||
|
value: 'One Hundred and One'
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Get all of the values/scores from the sorted set using
|
||||||
|
// the scan approach:
|
||||||
|
// https://redis.io/commands/zscan
|
||||||
|
for await (const memberWithScore of client.zScanIterator('mysortedset')) {
|
||||||
|
console.log(memberWithScore);
|
||||||
|
}
|
||||||
|
|
||||||
|
await client.quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
addToSortedSet();
|
Reference in New Issue
Block a user