user1707810
user1707810

Reputation: 123

Redis + Node.js - how do I retrieve the values

I am using a Redis database with Node.js. Using client.hmset("jobs", "jobId_12345", JSON.stringify(jsonJob)) I store JSON stringified jobs.

Now I want to iterate over all jobs and retrieve both job id and stringified job.

I tried client.hkeys("jobs", function (err, replies) {} but that only retrieves the keys.

I tried client.hgetall("jobs", function (err, obj) {} but I don't know how to retrieve both key and value from obj.

Any help is greatly appreciated because I'm stuck.

Upvotes: 1

Views: 3740

Answers (2)

user1707810
user1707810

Reputation: 123

This is how it works. id in the code below is the record id.

redisclient.hgetall(key, function (err, dbset) {

  // gather all records
  for (id in dbset) {
       ...
  }
}); 

Upvotes: 2

Tommaso Barbugli
Tommaso Barbugli

Reputation: 12031

Redis HGETALL is the right command (client.hgetall)

As you can also see in redis docs, HGETALL returns all fields and values of the hash stored for the requested key.

I am not 100% sure but client.hgetall should return a list with fields and values

[field1_name, field1_value, field2_name, field2_value ...]

Upvotes: 0

Related Questions