In red is using hash, I need to store hash key with multiple fields and values.
I tried as below:
client.hmset("Table1", "Id", "9324324", "ReqNo", "23432", redis.print);
client.hmset("Table1", "Id", "9324325", "ReqNo", "23432", redis.print);
var arrrep = new Array();
client.hgetall("Table1", function(err, rep){
console.log(rep);
});
Output is: { Id: '9324325', ReqNo: '23432' }
I am getting only one value. How to get all fields and values in the hash key? Kindly help me if I am wrong and let me get the code. Thanks.
You are getting one value because you override the previous value.
This adds Id, ReqNo to the Table1 hash object.
This overrides Id and ReqNo for the Table1 hash object. At this point, you only have two fields in the hash.
Actually, your problem comes form the fact you are trying to map a relational database model to Redis. You should not. With Redis, it is better to think in term of data structures and access paths.
You need to store one hash object per record. For instance:
Then, you can use a set to store the IDs:
Finally to retrieve the ReqNo data associated to the Table1 collection:
If you want to also search for all the IDs which are associated to a given ReqNo, then you need another structure to support this access path:
So you can get the list of IDs for record 23432 by using:
In other words, do not try to transpose a relational model: just create your own data structures supporting your use cases.