Will the LPUSH command work on a record that was initialized by JSON?

If a redis entry was installed from a JSON object that contains a list in it, can later use the LPUSH command in this list to update it? (using redis on node.js) Thanks

+3
source share
1 answer

No. Redis is an agnostic format. It cannot parse JSON, and it is not a document-oriented database.

LPUSH applies only to Redis list objects.

What can be done, but using the scripting capabilities on the server side of Lua Redis 2.6, is a script to decode a JSON object, add an element, encode the object and save it.

For example, with the following JSON object:

set users:1 "{\"id\":1,\"name\":\"Rocco\",\"age\":50,girlfriends:[\"Ulla\"]}"

eval:

eval "local t = cjson.decode( redis.call('get',KEYS[1] ))
  if t.girlfriends then
     table.insert(t.girlfriends,ARGV[1])
  else
     t.girlfriends = {ARGV[1]}
  end
  return redis.call('set',KEYS[1], cjson.encode(t))
" 1 user:1 Augusta

- JSON Redis, JSON Redis ( ).

:

 { "id":1, "name": "Rocco", "age":50,
  girlfriends: [ "Ulla", "Bella", "Josepha", "Isabella" ] }

:

 HMSET users:1:info name Rocco age 50
 RPUSH users:1:girlfriends Ulla Bella Josepha Isabella

. - , .

+8

All Articles