Ali Ali - 1 year ago 177
Node.js Question

Redis & Node.js: All keys

Basic question: Using Node.js I would like to get all the keys in my redis db. My redis db looks like this when I call keys *;

  1. aXF

  2. x9U

  3. lOk

So each record I have, has a unique key, generated as a random string. Now I would like to call something like foreach(key in Redis) and get all keys in the redis. Would it be possible to accomplish a "SELECT * FROM Redis"-like query with Node.js & Redis

Answer Source

Sure, you'll need to install the redis module for nodejs which can be found at

npm install node_redis


The above command is no longer available. you can use the following:

npm install redis

Then you would do:

var redis = require('redis'),
    client = redis.createClient();

client.keys('*', function (err, keys) {
  if (err) return console.log(err);

  for(var i = 0, len = keys.length; i < len; i++) {

Generally speaking you won't want to always return all of the keys (performance will be bad for larger data sets), but this will work if you are just testing things out. There is even a nice warning in the Redis documentation:

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using sets.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download