Skip to content
develephant edited this page Feb 4, 2016 · 2 revisions

Under development.

Methods:

Grid FS Object

:find_one

Parameters

Name Description Default Required
fields Unknown usage ? Yes

Returns

A gridfs file object.

local gridfs_file = gridfs:find_one( fields )

:find

Parameters

Name Description Default Required
fields Unknown usage ? Yes

Returns

A list of gridfs file objects.

local gridfs_files = gridfs:find( fields )
for _, file in ipairs( gridfs_files ) do
  print( file._id )
end

:remove

Parameters

Name Description Default Required
fields Unknown usage ? Yes
singleRemove Remove only the first result 0 No
safe Issues a cmd getlasterror 0 No

Returns

Number of files deleted, or nil with error message

local n, err = gridfs:remove(fields, singleRemove, safe)

:get

Writes the first matching file to file_handle. This API will malloc a buffer in file size in memory.

Parameters

Name Description Default Required
file_handle An open file handle none Yes
fields Fields to search for none Yes

Returns

On success returns True, otherwise False.

local bool = gridfs:get(file_handle, fields)

:insert

Insert a Grid FS file object.

Parameters

Name Description Default Required
file_handle A file handle returned from io.open none Yes
meta A file metatable (see below) meta.filename No
safe Issues a cmd getlasterror 0 No

Meta

The meta table can contain any additional information related to the file.

  • _id
  • filename
  • chunkSize
  • contentType
  • aliases
  • metadata (user data)

Returns

On success 0, or nil with error message.

local file_handle = io.open('some/file/path')
local n, err = gridfs:insert(file_handle, meta, safe)

:new

Creates a new Grid FS object.

Parameters

Name Description Default Required
meta A file metatable (see below) meta.filename Yes

Meta

The meta table can contain any additional information related to the file.

  • _id
  • filename
  • chunkSize
  • contentType
  • aliases
  • metadata (user data)

Returns

A gridfs file object, or nil with error message

local gridfs_file, err = gridfs:new(meta)

Grid FS File Object

:read

Read a file.

Parameters

Name Description Default Required
size The number of bytes to read none Yes
offset Position to start the read 0 No

Returns

Number of bytes read, or nil with error message.

local n, err = gridfs_file:read(size, offset)

:write

Write a file.

Parameters

Name Description Default Required
buf File buffer? none Yes
offset Position to start the write 0 No
size The number of bytes to be written none Yes

Returns

Returns number of bytes written, or nil with error message.

local n, err = gridfs_file:write(buf, offset, size)

:flush

Flushes cached data in memory to the database. Should always be called at the end of a file write.

Parameters

None

Returns

Nothing

gridfs_file:flush()

:update_md5

Hashes the file content and updates the md5 in file collection.

Parameters

None

Returns

True on success, or False and error message

local bool, err = gridfs_file:update_md5()
Clone this wiki locally