bigplum / lua-resty-mongol

A ngx_lua driver for mongodb -- Deprecated for not updating with the mongodb version
198 stars 67 forks source link

Dealing onlyarray in 'to_bson' function may cause data loss #52

Open Gerrard-YNWA opened 8 years ago

Gerrard-YNWA commented 8 years ago

As we know, when we iterate table with pairs in lua, it doesn't guarantee the order as we defines in table. http://www.lua.org/manual/5.2/manual.html#pdf-next

if onlyarray then
    if t_k == "number" and k >= 0 then
        if k >= high_n then
            high_n = k 
            seen_n [ k ] = v 
        end 
    else
        onlyarray = false
    end 
end

the code above deals array in to_bson in bson.lua, array value will store in seen_n only if k >= high_n, however this condition won't always be true. It means we may lost some value and results in 'null' stores in mongodb. I think seen_n [ k ] = v should be moved out of the if condition and we can hold all the data.

aaashun commented 8 years ago

https://github.com/aaashun/lua-resty-mongol/issues/5 closed