在节点中使用 redis 获取哈希键的所有字段和值 [英] Get all fields and values of hash key using redis in node

查看:63
本文介绍了在节点中使用 redis 获取哈希键的所有字段和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

红色是使用散列,我需要存储具有多个字段和值的散列键.我试过如下:

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);
});

输出为:{ 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.

client.hmset("Table1", "Id", "9324324", "ReqNo", "23432", redis.print);

这会将 Id、ReqNo 添加到 Table1 哈希对象中.

This adds Id, ReqNo to the Table1 hash object.

client.hmset("Table1", "Id", "9324325", "ReqNo", "23432", redis.print);

这将覆盖 Table1 哈希对象的 Id 和 ReqNo.此时,散列中只有两个字段.

This overrides Id and ReqNo for the Table1 hash object. At this point, you only have two fields in the hash.

实际上,您的问题源于您试图将关系数据库模型映射到 Redis.你不应该.使用Redis,最好从数据结构和访问路径方面考虑.

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:

HMSET Id:9324324 ReqNo 23432 ... and some other properties ...
HMSET Id:9324325 ReqNo 23432 ... and some other properties ...

然后,您可以使用集合来存储 ID:

Then, you can use a set to store the IDs:

SADD Table1 9324324 9324325

最后检索与 Table1 集合关联的 ReqNo 数据:

Finally to retrieve the ReqNo data associated to the Table1 collection:

SORT Table1 BY NOSORT GET # GET Id:*->ReqNo

如果您还想搜索与给定 ReqNo 关联的所有 ID,那么您需要另一个结构来支持此访问路径:

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:

SADD ReqNo:23432 9324324 9324325

因此您可以使用以下方法获取记录 23432 的 ID 列表:

So you can get the list of IDs for record 23432 by using:

SMEMBERS ReqNo:23432

换句话说,不要尝试转置关系模型:只需创建您自己的数据结构来支持您的用例.

In other words, do not try to transpose a relational model: just create your own data structures supporting your use cases.

这篇关于在节点中使用 redis 获取哈希键的所有字段和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆