简体   繁体   中英

Querying in redis from node.js

I am coming from a traditional SQL background and am learning Redis. Specifically I am trying to run a redis database along with node.js

I have got the initial setup of redis done and tried out few of their basic commands. However I would like to create a database which has a Id, 3 marks of students, start date and end date.

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.

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. However I would like to get values for the ID from a particular start date and end date. In SQL it would be like: SELECT * FROM TABLE WHERE StartDATE>='' and endDate='' and id= ID;

What would be the alternative in redis? I haven't been able to find any query example for redis in node.js

Also while inserting into the database, is inserting the object as a whole the best way to go about?

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.

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. Stealing from the node_redis examples:

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

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. 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). 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. OTOH, this approach could prove challenging if you're trying to reassemble a JSON from the Hash data type.

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. One way to do what you're looking for is with Redis' Sorted Sets. 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). Then, to get all IDs between two given dates, use 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/

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://github.com/redis/redis-om-node

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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