A BSON library for lua
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
LICENSE fix typo Jun 17, 2013
Makefile add LUAMOD_API Mar 16, 2017
README.md bson.binary May 4, 2017
bson.c destroy bson object when encode error Apr 20, 2017
test.lua destroy bson object when encode error Apr 20, 2017
utils.lua 增加对__pairs的table的支持 May 19, 2016



This is a simple BSON library for Lua.


make win


make linux


Lua TypeBSON TypeNotes
number32-bit integer
number64-bit integerprecision lost in lua 5.1/5.2
bson.binary(blob)Binary stringUse bson.type() to decode
tableBSON Document
tableBSON ArrayLua table must be a sequence. (Continuous number key base 1)
bson.date(os.time())UTC Datetimemilliseconds since epoch
bson.timestamp(os.time())Timestampspecial mongodb type, two 32-bit number
bson.regex(regex,option)Regular Expression
bson.objectid()ObjectIDMongoDB document ID
bson.minkeyMin Key
bson.maxkeyMax Key

Convert a bson value to lua string

A value with bson type Null,Binary,MinKey,Maxkey,etc would be encoded in lua, you can use bson.type(v) to convent back to normal lua string.

typestring, value = bson.type(bson.binary("Hello"))
assert(typestring == "binary")
assert(value == "Hello")

Replace field

These bson types (fixed length) below can be replace by new value after encode to bson object.

  • int32
  • int64
  • double
  • boolean
  • date
  • timestamp
  • objectid

You need call makeindex() before replace.

Getting started

See test.lua