简体   繁体   English

从 node.js 查询 redis

[英]Querying in redis from node.js

I am coming from a traditional SQL background and am learning Redis.我来自传统的 SQL 背景,正在学习 Redis。 Specifically I am trying to run a redis database along with node.js具体来说,我正在尝试运行 redis 数据库以及 node.js

I have got the initial setup of redis done and tried out few of their basic commands.我已经完成了 redis 的初始设置并尝试了一些基本命令。 However I would like to create a database which has a Id, 3 marks of students, start date and end date.但是我想创建一个数据库,它有一个 ID、3 个学生标记、开始日期和结束日期。

I believe I can create an object which stores all the values for a particular ID (say obj ) and use set function to save details regarding that ID.我相信我可以创建一个 object 来存储特定 ID(比如obj )的所有值,并使用 set function 来保存有关该 ID 的详细信息。

client.set(ID, obj, function(err, reply) {
  console.log(reply);
});

If I call client.get(ID) , it will return the object which I saved above.如果我调用client.get(ID) ,它将返回我在上面保存的 object 。 However I would like to get values for the ID from a particular start date and end date.但是,我想从特定的开始日期和结束日期获取 ID 的值。 In SQL it would be like: SELECT * FROM TABLE WHERE StartDATE>='' and endDate='' and id= ID;在 SQL 中,它会像: SELECT * FROM TABLE WHERE StartDATE>='' and endDate='' and id= ID;

What would be the alternative in redis? redis 中的替代方案是什么? I haven't been able to find any query example for redis in node.js我无法在 node.js 中找到 redis 的任何查询示例

Also while inserting into the database, is inserting the object as a whole the best way to go about?另外在插入数据库时,将 object 作为一个整体插入 go 的最佳方法是什么?

Also while inserting into the database, is inserting the object as a whole the best way to go about? 另外,在插入数据库时​​,将对象整体插入是最好的方法吗?

No, not for the sample query you propose. 不,不是您建议的示例查询。 Redis is not a traditional database, it's more like a key value store , it doesn't understand your object's structure. Redis不是传统的数据库,它更像是一个键值存储 ,它不了解对象的结构。

If you want to make queries like you suggest, you might be using the wrong tool. 如果要按照您的建议进行查询,则可能使用了错误的工具。

In redis you wouldn't store an object, but you could store simpler structures, like a hash. 在redis中,您不会存储对象,但是可以存储更简单的结构,例如哈希。 Stealing from the node_redis examples: node_redis示例中窃取:

client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234");
client.hgetall("hosts", function (err, obj) {
    console.dir(obj);
});

Output: 输出:

{ mjr: '1', another: '23', home: '1234' }

For more details, check out the redis docs here: http://redis.io/commands 有关更多详细信息,请在此处查看redis文档: http ://redis.io/commands

This question is actually made up of two: storing objects and indexing. 这个问题实际上由两个部分组成:存储对象和索引。

You can store objects in Redis in many different ways - the trick is to find the data structures that fit your data and the operations on it best. 您可以通过多种方式将对象存储在Redis中-诀窍是找到最适合您的数据的数据结构以及对其进行最佳操作。 Storing your JSON string as is in a Redis String is dead simple, but note that any update will require parsing the entire JSON, making the modification and then rewriting the whole thing (tip: if you go this way, look into Redis' Lua scripts and specifically learn to use the cjson library - it could be invaluable in this context). 按原样存储Redis字符串中的JSON字符串非常简单,但是请注意,任何更新都需要解析整个JSON,进行修改,然后重写整个内容(提示:如果您这样做,请查看Redis的Lua脚本并专门学习使用cjson库-在这种情况下它可能是无价的)。 Alternatively, you could break your objects into Redis Hashes - this is slightly more complicated as it requires some manual mapping, but allows field-level access. 或者,您可以将对象分解为Redis哈希-这稍微复杂一点,因为它需要一些手动映射,但允许字段级访问。 OTOH, this approach could prove challenging if you're trying to reassemble a JSON from the Hash data type. OTOH,如果您尝试从Hash数据类型重组JSON,则此方法可能会遇到挑战。

Redis doesn't have built-in indices so to fetch data by specific properties (eg creation date) you'll need to create and maintain index-like data structures in Redis. Redis没有内置索引,因此要通过特定属性(例如创建日期)来获取数据,您需要在Redis中创建并维护类似于索引的数据结构。 One way to do what you're looking for is with Redis' Sorted Sets. 做您要寻找的一种方法是使用Redis的排序集。 Keep a sorted set with all your ( obj ) IDs as members, and set each member's score as its creation date (in unix epoch for example). 保留一个以您所有( obj )ID作为成员的排序集,并将每个成员的得分设置为其创建日期(例如,在unix时代)。 Then, to get all IDs between two given dates, use Redis' ZRANGEBYSCORE . 然后,要获取两个给定日期之间的所有ID,请使用Redis的ZRANGEBYSCORE

More pointers: 更多指针:

You can follow given example: 您可以按照给出的示例进行操作:

var redis = require('redis');
var port=13235;//Your Redis cloud port number
var host='redis-13235.c14.us-east-11-12.ec4.cloud.redislabs.com';//Your redis cloud host
var pass='password';//Redis cloud password
var client = redis.createClient(port,host, {no_ready_check: true});
client.auth(pass, function (err) {
    if (err) throw err;
});
client.on('connect', function() {
    console.log('connected......\n welcome to redis cloud !!!');
});

for(var i=0;i<10;i++){
client.set('TEST_TIME'+i,new Date().getTime());
}
for(var i=0;i<10;i++){
client.get('TEST_TIME'+i, function (err, reply) {
    console.log(reply.toString()); 
});
}

Check below link for more info: https://www.sitepoint.com/using-redis-node-js/ 检查以下链接以了解更多信息: https : //www.sitepoint.com/using-redis-node-js/

For people still looking for a solution, you should know that since Nov 2021, Redis has introduced Redis OM: an object mapping library for Redis For people still looking for a solution, you should know that since Nov 2021, Redis has introduced Redis OM: an object mapping library for Redis

https://redis.io/docs/stack/get-started/tutorials/stack-node/ https://redis.io/docs/stack/get-started/tutorials/stack-node/

https://github.com/redis/redis-om-node https://github.com/redis/redis-om-node

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM