flybass / luamongo

Automatically exported from code.google.com/p/luamongo
0 stars 0 forks source link

[Feature] Add API to inserted ordered documents #5

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Lua orders keys in a table arbitrarily. In MongoDB documents with the same 
keys, but in a different order, are considered distinct.

For some purposes, the current Lua API is not enough and a particular order on 
the documents needs to be imposed. Therefore I would like to request the 
addition of an API which allows this. I couldn't find that this currently 
exists. Please point me into the right direction if it is already possible.

An idea could be to have a specific "insert_sorted" function which takes a 
(array) table of tables. The second order tables would be arrays with two 
elements with key and value. Double keys should trigger an error (a table of 
accepted keys must be maintained during the insert). Sub-documents themselves 
must again follow this strict format.
An alternative could be to have a specific __bsontype, like "ordered" document. 
The value of such a datatype is the format outlined above. But for 
sub-documents and values we do not need to enforce the same format, an 
unordered document could be used as sub-document.
The latter solution has the benefit of being able to use the same insert 
function, and mix ordered and unordered documents if desired.

Original issue reported on code.google.com by niemueller on 5 Nov 2010 at 3:39

GoogleCodeExporter commented 8 years ago
I've pondered this a bit too.   I like your __bsontype alternative.  I also 
proposed a similar solution for Arrays, which right now are implicitly created.

I thought about three different forms for the __bsontype approach:
mongo.Ordered{ {key1 = value1}, {key2 = value2} }
mongo.Ordered( key1, value1, key2, value2 )  -- error if there are odd # of 
arguments
mongo.Ordered( {key1 = value1, key2 = value2}, {key1, key2} )  -- second table 
is order of keys

Original comment by neoman...@gmail.com on 9 Nov 2010 at 1:51

GoogleCodeExporter commented 8 years ago
I think I'd prefer option 1 for the API, where values can be arbitrary types 
again, so also complex __bsontype tables.

Original comment by niemueller on 12 Nov 2010 at 5:09