Search code examples
luauser-defined-functionslua-tableaerospike

changing the value of bin in a record of aerospike db which is of map type in lua script


say aerospike database is having recorded data like below

let the namespace be employee

name age characteristics  
sachin 25 MAP('{"weight":70, "height":25}')  

now i want to change the value of height which is inside map for all the records in employee namespace through lua script.

i have tried for changing the bins of normal data type as below , i,e i tried to change the age as below:

function changeAgeOfEmployee(rec)
  if not aerospike:exists(rec) then
     error ("Invalid Record. Returning")
     return
  else
     age = 30
     rec['age'] = age
     aerospike:update(rec)
  end
end

but i am not sure how to change the value in map in lua, can somebody please assist me in this


Solution

  • Your MAP data-type is basically a lua table. MAP in lua can be written as:

    local m = map {"weight" => 70, "height" => 25}
    

    To loop over all the key/value pairs you should use the pairs iterator like this:

    for key, value in map.pairs(m) do
        m[key] = 30 --this changes all the values of your MAP to 30
    end