local mongo = require 'mongo'
Returns the type of value
as a string.
print(mongo.type(mongo.Int32(123)))
print(mongo.type(mongo.Null))
print(mongo.type('abc'))
Output:
mongo.Int32
mongo.Null
string
Returns an instance of BSON Binary.
Returns an instance of BSON document constructed from value
that can be one of the following:
- a BSON document in which case this method does nothing;
- a string in BSON or JSON format;
- a table (without a metatable);
- a table or userdata with a
__toBSON
metamethod that returns a value, BSON type or BSON document;
Note that non-numeric keys are unordered in Lua tables. As a result, it is impossible to ensure a specific order for fields in a BSON document constructed from a table. When strict order is required, the following workaround can be used:
print(mongo.BSON{a = 1, b = 2, c = 3}) -- From table (order is unspecified)
print(mongo.BSON('{ "a" : 1, "b" : 2, "c" : 3 }')) -- From JSON (order is preserved)
-- Add fields incrementally
local bson = mongo.BSON('{}')
bson:append('a', 1)
bson:append('b', 2)
bson:append('c', 3)
print(bson)
Output:
{ "a" : 1, "c" : 3, "b" : 2 }
{ "a" : 1, "b" : 2, "c" : 3 }
{ "a" : 1, "b" : 2, "c" : 3 }
A table (root or nested) is converted to an array if it has a field __array
whose value is
true. The length of the resulting array can be adjusted by storing an integer value in that field.
Otherwise, it is assumed to be equal to the raw length of the table.
print(mongo.BSON{a = {__array = true, 1, 2, 3}})
print(mongo.BSON{a = {__array = true, nil, 1, nil, 2, nil}})
print(mongo.BSON{a = {__array = 3, nil, 1, nil, 2, nil}})
Output:
{ "a" : [ 1, 2, 3 ] }
{ "a" : [ null, 1 ] }
{ "a" : [ null, 1, null ] }
Returns a new Client handle. See also MongoDB Connection String URI Format for information on uri
.
Returns an instance of BSON DateTime.
Returns an instance of BSON Decimal128.
Returns an instance of BSON Double. This type can be used to override automatic number conversion where needed.
Returns an instance of BSON Int32. This type can be used to override automatic number conversion where needed.
Returns an instance of BSON Int64. This type can be used to override automatic number conversion where needed.
Returns an instance of BSON Javascript with scope
(converted to a BSON document).
Returns an instance of BSON ObjectID. Optional hexadecimal string value
is used to initialize
the instance. Otherwise, a new unique value is generated.
Returns an instance of read preferences with mode
(a string) that can be one of the following:
primary
primaryPreferred
secondary
secondaryPreferred
nearest
Refer to http://mongoc.org/libmongoc/current/mongoc_read_mode_t.html for more information.
Returns an instance of BSON Regex.
Returns an instance of BSON Timestamp.
The BSON MaxKey singleton object.
The BSON MinKey singleton object.
The BSON Null singleton object.