Dragonfly Cloud announces new enterprise security features - learn more

Dragonfly

Getting a List in Node.js using Redis (Detailed Guide w/ Code Examples)

Use Case(s)

Retrieving a list from Redis is a common use case when you need to get an array of elements saved previously. These lists can serve as message queues, timelines, or even just a simple stack or queue.

Code Examples

The following example demonstrates how to retrieve a list in Node.js using the redis module:

```
const redis = require('redis');
const client = redis.createClient();

client.lrange('mylist', 0, -1, function(err, reply) {
console.log(reply);
});
```
In this code:

Another example shows how to get only the first three items from the list:

```
client.lrange('mylist', 0, 2, function(err, reply) {
console.log(reply);
});
```

Best Practices

Common Mistakes

FAQs

Q: What does -1 mean in the lrange command?
A: In the context of the Redis lrange command, -1 means 'until the end of the list'.

Was this content helpful?

Similar Code Examples

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost