0

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?

asked May 5, 2015 at 23:02

4 Answers 4

3

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

answered May 6, 2015 at 0:55
Sign up to request clarification or add additional context in comments.

2 Comments

my objects were in JSON format. I converted it into a string and entered into the database. Could you guide me how I could query by a specific date?
You can't. Redis doesn't understand your JSON string. To be able to query by date, you would need to convert your dates into string keys that you would map to the object ID, or store them as sorted sets to make range queries. In short, you should probably use a different database. Postgres and MongoDB are popular options that both support storing JSON documents and querying their properties.
1

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 (e.g. 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:

answered May 6, 2015 at 8:17

Comments

1

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

answered Jul 29, 2022 at 23:48

Comments

-1

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/

answered Feb 5, 2018 at 7:30

1 Comment

@chade_ Thanks for suggestion. i have removed image and posted code instead with little bit modification.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.