Skip to content
Paco Zamora Martinez edited this page Dec 23, 2015 · 5 revisions

##Methods mongo.GridFileBuilder.New(gridfs_instance)

builder, err = mongo.GridFileBuilder.New(gridfs_instance)

append(data)

ok, err = builder:append([string])

write(data)

ok, err = builder:write([string])

It is an alias of builder:append(string).


build(remote_filename)

ok, err = gridfs:build([remote_filename])

###Example

local mongo = require('mongo')

-- Create a connection object
local db = assert(mongo.Connection.New())

-- connect to the server on localhost
assert(db:connect('localhost'))

-- create a GridFileBuilder handle
local gridfs = mongo.GridFS.New(db, 'test')
local builder = assert(mongo.GridFileBuilder.New(gridfs) )

-- example loop generating data on-the-fly
for i=1,100 do
    assert(builder:append(string.format("Line number %d\n", i)))
end

-- set the name of the file and it will be visible in GridFS
assert(builder:build('myfile.txt'))

-- create a GridFS handle
local gridfs = assert(mongo.GridFS.New(db, 'test'))

local gridfile = assert(gridfs:find_file('myfile.txt'))

-- print file content
for i=1,gridfile:num_chunks() do
    local chunk = assert(gridfile:chunk(i-1))
    io.write(chunk:data())
end
Clone this wiki locally