From ac7d50c7313fe1158131e13b0e3b11d982697123 Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Thu, 24 Feb 2022 23:04:06 +0000 Subject: [PATCH] Added sorted set example. (#2005) * Added sorted set example. * Update sorted-set.js Co-authored-by: Leibale Eidelman --- examples/README.md | 1 + examples/sorted-set.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 examples/sorted-set.js diff --git a/examples/README.md b/examples/README.md index c3a54c1102..eb9a0e4325 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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-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 | +| `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-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) | diff --git a/examples/sorted-set.js b/examples/sorted-set.js new file mode 100644 index 0000000000..e2c4f14511 --- /dev/null +++ b/examples/sorted-set.js @@ -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();