Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save many json objects in redis database using node.js

In my application im using node.js with redis database.How can i save many json objects in redis.

            db.save({
                description:'sdsd',userId:'324324',url:'http://www.abc.com/abc.html',appId:'123456'
          }, function (err, res) {
              if(err){
                  console.log(err);
                  return;
              }
              else{

                  console.log(res);
              }
          });

In couch db we can save the json objects again and again as document by using the above code.How to do this in redis.From their documentation i came to know below code save the json objects

               client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234");

Again i want to save the other json object in same "hosts" like below

          client.hmset("hosts", "mjr", "2", "another", "33", "home", "1235");

How can i do this.

like image 458
sachin Avatar asked Apr 24 '26 20:04

sachin


1 Answers

Redis storage model is different from CouchDB. In Redis, everything gets accessed by its key, so it all depends how you plan to retrieve your data.

So if you'd like to be able to retrieve data by userId, use this as the key.

redis.set('324324', {
  description:'sdsd',
  url:'http://www.abc.com/abc.html',
  appId:'123456'
  });

But if you need to retrieve a piece of data using more than one piece of the data, then redis may not be suitable.

In some cases, you may be able to use some tricks, so that to be able to query on both userId and appId, you could use 324324:123456 as the key, and query using

GET 324324:*

to get all apps for one user

or

GET *:123456

to get all users for a given app.

like image 166
Pascal Belloncle Avatar answered Apr 30 '26 03:04

Pascal Belloncle