Skip to content

Added sorted set example. #2005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
35 changes: 35 additions & 0 deletions examples/sorted-set.js
Original file line number Diff line number Diff line change
@@ -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();