Redis & Node.js: All keys
npm install node_redis
is no more available now. Use this instead -
npm install redis
Sure, you'll need to install the redis
module for nodejs
which can be found at https://github.com/redis/node-redis.
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++) {
console.log(keys[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.
Node-Redis v4 Update:
The redis
package for node on npm has recently seen a major update (v4), and there's a much easier way to accomplish this.
Using the sendCommand
function, you can run any redis command you wish without needing a dedicated function for it in the package, and can retrieve the output.
const keys = await client.sendCommand(["keys","*"]);
console.log(keys); // ["aXF","x9U","lOk",...]
Note:
Please do consider the point mentioned in Bill's answer:
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.
Install redis client for nodejs
npm install redis
Then I do the following to get all key's data
var redis = require('redis'),
client = redis.createClient();
client.multi()
.keys('*', function (err, replies) {
// NOTE: code in this callback is NOT atomic
// this only happens after the the .exec call finishes.
console.log("MULTI got " + replies.length + " replies");
replies.forEach(function (reply, index) {
console.log("Reply " + index + ": " + reply.toString());
client.get(reply, function(err, data){
console.log(data);
});
});
})
.exec(function (err, replies) {});