Lua bindings to MongoDB's libbson
- Q: Is it compatible with cjson?
A: Hell no. All bson subtypes are defined as userdata. Use cbson.to_json and cbson.from_json - Q: What about integers?
A: Lua, prior to 5.3 stores all numbers as double. So, cbson.encode({foo = 10}) will yield double bson type.
Use cbson.int datatype. It will automaticaly yield int32 or int64 depending on value.
- Lua (5.1/5.2) or LuaJit
- Cmake 2.8.12 or later
- Working C compiler
- libbson 1.7 or later
As easy as
mkdir build
cd build
cmake ..
make
make install
You can also use make unittest
after make to run tests.
By default module compiles with support for luajit
For other Lua interpreters see cmake options.
Via rockspec-file:
luarocks install https://raw.githubusercontent.com/isage/lua-cbson/master/rockspec/lua-cbson-git-1.rockspec
use specific lua version or luajit
luarocks --lua-dir=/usr/local/opt/openresty/luajit install https://raw.githubusercontent.com/isage/lua-cbson/master/rockspec/lua-cbson-git-1.rockspec
See all available versions of lua-bson in [./rockspec](rockspec directory).
local cbson = require "cbson"
local table_data = cbson.decode(bson_data)
local bson_data = cbson.encode(table_data)
local json_string = cbson.to_json(bson_data)
local json_string = cbson.to_relaxed_json(bson_data)
local bson_data = cbson.from_json(json_string)
cjson
(2.1.0.5 and higher) uses metatable for set table (especially empty) as arrays, so this library can use this (or other)
metatable for encoding and decoding arrays. See usage array metatables example
Decodes binary BSON data to lua table.
Encodes lua table to binary BSON data.
local cbson = require "cbson"
-- Encodes lua table
local bson_data = cbson.encode({foobar = {bar = "hello", foo = "world"}})
print(cbson.to_json(bson_data)) -- { "foobar" : { "foo" : "world", "bar" : "hello" } }
-- Encodes lua table containing bson data
local bson_data = cbson.encode({foobar = cbson.from_json('{"bar":"hello","foo":"world"}')})
print(cbson.to_json(bson_data)) -- { "foobar" : { "bar" : "hello", "foo" : "world" } }
Encodes lua table to binary BSON data, putting first_key value at start of bson.
(required for mongodb commands)
Make sure, that given key exists, otherwise it'll add this key with NULL value.
Encodes json string as binary BSON data.
Encodes binary BSON data as json string.
Encodes binary BSON data as relaxed json string.
local regex = cbson.regex("foo|bar","ism")
print( regex:regex() )
regex:regex("bar|foo")
regex:options("im")
print( regex:options() )
Unique object id with 24 hex numbers.
local oid = cbson.oid("1234567890ABCDEF01234567")
local oid2 = cbson.oid(oid)
Get time from oid
(first 8 hex numbers):
local timestamp = oid:timestamp()
print(timestamp) -- 305419896
local binary = cbson.binary("ZGVhZGJlZWY=")
print( binary:raw() ) -- returns raw binary data "deadbeef"
binary:set_raw("hello") -- binary:raw("hello") works fine
print( binary:data() ) -- returns base64 encoded data "aGVsbG8="
binary:set_data("ZGVhZGJlZWY=")
Maximum size of binary string is 4,294,967,295
bytes.
local symbol = cbson.symbol("something")
local code = cbson.code("code")
Scopes are not supported yet.
local code = cbson.codewcope("code")
local undef = cbson.undefined()
local null = cbson.null()
local empty_array = cbson.array()
local minkey = cbson.minkey()
local maxkey = cbson.maxkey()
local ref = cbson.ref("foo", "123456789012345678901234")
print( ref:ref() )
ref:ref("bar")
ref:id("432109876543210987654321")
BSON has a special timestamp
type for internal
MongoDB use and is not associated with the regular date
type.
local timestamp = cbson.timestamp(100,1000)
timestamp:timestamp(500)
print( timestamp:timestamp() ) -- 500
timestamp:increment(100500)
print( timestamp:increment() ) -- 100500
That's basically int64. It supports all arithmetic operations.
Due to nature of lua numbers, you can pass big (> 2^53) numbers only as string.
It also have __tostring metamethod for opposite operation.
There's also helper methods int_to_raw/uint_to_raw(<int>value, <number>bytes, <bool>big_endian)
and raw_to_int/raw_to_uint(<string>data, <bool>big_endian)
for converting between number and in-memory representation.
local int = cbson.int(10)
print(int) -- 10
local int = cbson.int("100")
print(int) -- 100
local int = cbson.int("100")
print(int:number() + 20) -- 120
local int = cbson.int(10)
local int2 = cbson.int(int)
print(int2) -- 10
See cbson.int. They are same (except sign), and you can init one from another.
BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.
Since version 1.1
.
It's decimal128.
The Decimal128 supports 34 decimal digits of precision,
a max value of approximately 10^6145
, and min value of approximately -10^6145
local dec = cbson.decimal("1.0")
print(dec) -- 1.0
local dec = cbson.decimal("0.0005")
print(dec) -- 0.0005
Epifanov Ivan [email protected]
This module is licensed under the WTFPL license.
(See LICENSE)