lua-mongo icon indicating copy to clipboard operation
lua-mongo copied to clipboard

Cannot create bson document with array indexed by numbers

Open cat-anna opened this issue 1 year ago • 3 comments

Hello

It looks like it is not possible to create bson document with number indexed arrays:

lua: mongotest.lua:5: bad argument #1 to 'BSON' (["path"] => string index expected, got number)
stack traceback:
        [C]: in function 'BSON'
        mongotest.lua:5: in main chunk
        [C]: ?

test:

local mongo = require 'mongo'
--works
mongo.BSON{ path = { ["1"]="a", ["2"]="b", ["3"]="c", }}
--fails
mongo.BSON{ path = { "a", "b", "c" } }

cat-anna avatar Aug 07 '22 08:08 cat-anna

It's possible. Have you read the docs before opening the issue?

https://github.com/neoxic/lua-mongo/blob/master/doc/main.md#mongobsonvalue

neoxic avatar Aug 07 '22 16:08 neoxic

Oh, I missed that adding __array is required. It's poor requirement. Few json libs I had worked with was able to handle it transparently. I'm replacing simple json storage with mongo and data is not tagged with __array nor it will be generated with it. So it seems I have to tag arrays manually before passing to lua-mongo.

cat-anna avatar Aug 09 '22 08:08 cat-anna

It's a very powerful requirement that helps avoid ambiguity. Let's suppose you have a table {[1] = 1, [3] = 3, [5] = 5} that maps one group of IDs to another. If treated automatically, it needs to be stored as {"1": 1, "3": 3, "5": 5} in JSON/BSON. And then it happens that two pairs are added to it - [2] = 2 and [4] = 4. Oops! Your table has suddenly become an indexed array in Lua and, if treated automatically, will be stored in JSON/BSON incorrectly as [1, 2, 3, 4, 5].

On the other hand, prohibiting automatic numeric key conversions and having an explicit indexed array marker __array helps avoid a great deal to subtle bugs like that.

Furthermore, if you wish to mark your tables with __array automatically on the fly, you can always do that using the __toBSON metamethod. This way, you are free to implement any indexed array autodetection policy as you see fit. It is much more flexible than having a "one size fits all" built-in strategy for detecting indexed arrays.

neoxic avatar Aug 09 '22 17:08 neoxic