-
Notifications
You must be signed in to change notification settings - Fork 13
/
bson-example.lua
53 lines (49 loc) · 1.27 KB
/
bson-example.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
bson = require 'bson'
epoch=os.time({year=1970, month=1, day=1, hour=0})
bsondoc1=bson.encode{year=2013,
month="April",
day=2
}
bsondoc2=bson.encode{username="maroc",
info={first="todd",
last="coram",
age=46,
signature1=bson.binary("\000\001\002\003",bson.B_GENERIC),
signature2=bson.binary("\000\001\002\003"),
saved=true,
now=bson.utc_datetime(),
past=bson.utc_datetime(epoch * 1000),
colors={"Red","Green","Blue"}
}}
bsondoc3=bson.encode{precise_age=22.9,
temperature=-11.3,
height=7.6
}
f=io.open("/tmp/test.bson", "wb")
f:write(bsondoc1)
f:write(bsondoc2)
f:write(bsondoc3)
f:close()
function print_table(t)
function printTableHelper(t, spacing)
for k,v in pairs(t) do
print(spacing..tostring(k), v)
if type(v) == "table" then
printTableHelper(v, spacing.."\t")
end
end
end
printTableHelper(t, "");
end
-- print_table(bson.decode(bsondoc1))
-- print_table(bson.decode(bsondoc2))
-- print_table(bson.decode(bsondoc3))
f=io.open("/tmp/test.bson", "rb")
while true do
local btab = bson.decode_next_io(f)
if not btab then break end
print("--Doc--")
print_table(btab)
print()
end
f:close()