lua-bson
lua-bson copied to clipboard
A BSON library for lua
Introduction
This is a simple BSON library for Lua.
Building
make win
or
make linux
Types
| Lua Type | BSON Type | Notes |
|---|---|---|
| bson.null | Null | |
| boolean | Boolean | |
| number | Double | |
| number | 32-bit integer | |
| number | 64-bit integer | precision lost in lua 5.1/5.2 |
| string | String | |
| bson.binary(blob) | Binary string | Use bson.type() to decode |
| table | BSON Document | |
| table | BSON Array | Lua table must be a sequence. (Continuous number key base 1) |
| bson.date(os.time()) | UTC Datetime | milliseconds since epoch |
| bson.timestamp(os.time()) | Timestamp | special mongodb type, two 32-bit number |
| bson.regex(regex,option) | Regular Expression | |
| bson.objectid() | ObjectID | MongoDB document ID |
| bson.minkey | Min Key | |
| bson.maxkey | Max 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