isage / lua-resty-moongoo

MongoDB library for OpenResty
Do What The F*ck You Want To Public License
117 stars 33 forks source link

Number returned as userdata. #28

Closed StartAt24 closed 5 years ago

StartAt24 commented 5 years ago

If the data type is number, "find" will returns a userdata。 So i have to convert it to number like tonumber(tostring(xxxx))。 It's not convenient. Is there an easy way to do this? Or can i get the right type of the data as it stored in mongodb?

isage commented 5 years ago

See https://github.com/isage/lua-resty-moongoo#note There's no such datatype as "number" in mongo. It's either int32, int64 or double. Lua number datatype is always double, that's just how it is (except in Lua 5.3, but this library targets lua 5.1/luajit). So there's no way to map true int32/int64 to lua number, thus userdata, and using tonumber(tostring(xxxx)) is discouraged.

Use cbson types for comparison, etc. use tostring for output.

StartAt24 commented 5 years ago

Thanks for your reply. Replace tonumber(tostring()) with userdata:number()

isage commented 5 years ago

It'll still fail on numbers > 2^53

On Wed, 13 Mar 2019, 14:58 StartAt24, notifications@github.com wrote:

Thanks for your reply. Replace tonumber(tostring()) with userdata:number()

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/isage/lua-resty-moongoo/issues/28#issuecomment-472392977, or mute the thread https://github.com/notifications/unsubscribe-auth/AAQtIcdQwdPZ3LUch8OVgcFeiomcGcqRks5vWOf2gaJpZM4bs_6B .