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

##Methods mongo.GridFS.New(connection, dbname, prefix)

gridfs, err = mongo.GridFS.New([connection obj], [dbname][, prefix])

find_file(query)

gridfile, err = gridfs:find_file([query])

query could be a Lua string or a Lua table.


find_file_by_name(filename)

gridfile, err = gridfs:find_file_by_name([filename])

filename could be a Lua string.


list()

cursor, err = gridfs:list()

remove_file(filename)

ok, err = gridfs:remove_file([filename])

filename must be a Lua string.


store_file(filename, remote_file, content_type)

gridfile, err = gridfs:store_file(filename[, remote_file[, content_type]])

gridfile:chunk(chunk_num)

chunk, err = gridfile:chunk([chunk_num])

gridfile:chunk_size()

chunk_size = gridfile:chunk_size()

gridfile:content_length()

content_length = gridfile:content_length()

gridfile:exists()

bool = gridfile:exists()

gridfile:filename()

filename_str = gridfile:filename()

gridfile:md5()

md5_str = gridfile:md5()

gridfile:metadata()

metadata_table = gridfile:metadata()

gridfile:num_chunks()

num_chunks = gridfile:num_chunks()

gridfile:upload_date()

date = gridfile:upload_date()

gridfile:write(filename)

success,err = gridfile:write([filename])

chunk:data()

str = chunk:data()

chunk:len()

length = chunk:len()

###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 GridFS handle
local gridfs = assert(mongo.GridFS.New(db, 'test'))

-- store a file on the server
local gridfile = assert(gridfs:store_file('/path/to/file'))

-- print details
print(gridfile:num_chunks())
print(gridfile:chunk_size())
print(gridfile:md5())
print(gridfile:metadata())
print(gridfile:upload_date())
print(#gridfile) -- synonym for gridfile:content_length()

-- examine file chunks
local chunk = assert(gridfile:chunk(0))
print(#chunk) -- synonym for chunk:len()
print(chunk:data())

-- write file data
assert(gridfile:write('/path/to/newfile'))

-- list files on the DB
local q = gridfs:list()
for gf in q:results() do
    print(gf:field('filename'))
end
Clone this wiki locally