Skip to content

Commit ac7d50c

Browse files
Simon Prickettleibale
Simon Prickett
andauthored
Added sorted set example. (#2005)
* Added sorted set example. * Update sorted-set.js Co-authored-by: Leibale Eidelman <[email protected]>
1 parent 00b58e8 commit ac7d50c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This folder contains example scripts showing how to use Node Redis in different
1515
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
1616
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
1717
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
18+
| `sorted-set.js` | Add members with scores to a Sorted Set and retrieve them using the ZSCAN iteractor functionality |
1819
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command |
1920
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command |
2021
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io) |

examples/sorted-set.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Add several values with their scores to a Sorted Set,
2+
// then retrieve them all using ZSCAN.
3+
4+
import { createClient } from 'redis';
5+
6+
async function addToSortedSet() {
7+
const client = createClient();
8+
await client.connect();
9+
10+
await client.zAdd('mysortedset', [
11+
{
12+
score: 99,
13+
value: 'Ninety Nine'
14+
},
15+
{
16+
score: 100,
17+
value: 'One Hundred'
18+
},
19+
{
20+
score: 101,
21+
value: 'One Hundred and One'
22+
}
23+
]);
24+
25+
// Get all of the values/scores from the sorted set using
26+
// the scan approach:
27+
// https://redis.io/commands/zscan
28+
for await (const memberWithScore of client.zScanIterator('mysortedset')) {
29+
console.log(memberWithScore);
30+
}
31+
32+
await client.quit();
33+
}
34+
35+
addToSortedSet();

0 commit comments

Comments
 (0)