Sorry for long time no maintaining. If you want to continue update it, please contact me.
lua-resty-mongol - Lua Mongodb driver for ngx_lua base on the cosocket API
Thanks to project Mongol by daurnimator
luajit(or attempt to yield across metamethod/C-call boundary error
will be produced.)
ngx_lua 0.5.0rc5 or ngx_openresty 1.0.11.7 is required.
make install
Add package path into nginx.conf.
lua_package_path '/usr/local/openresty/lualib/?/init.lua;;';
or into lua files before requiring.
local p = "/usr/local/openresty/lualib/"
local m_package_path = package.path
package.path = string.format("%s?.lua;%s?/init.lua;%s",
p, p, m_package_path)
Requring the module will return a function that connects to mongod: it takes a host (default localhost) and a port (default 27017); it returns a connection object.
mongol = require "resty.mongol"
conn = mongol:new() -- return a conntion object
Default host and port is: localhost
and 27017
.
Sets socket connecting, reading, writing timeout value, unit is milliseconds.
In case of success, returns 1. In case of errors, returns nil with a string describing the error.
Keeps the socket alive for msec
by ngx_lua cosocket.
In case of success, returns 1. In case of errors, returns nil with a string describing the error.
Returns the socket reused times.
In case of success, returns times. In case of errors, returns nil with a string describing the error.
Closes the connection.
In case of success, returns 1. In case of errors, returns nil with a string describing the error.
Returns a boolean indicating if this is the master server and a table of other hosts this server is replicating with
or nil, err
on failure.
Returns a new connection object that is connected to the primary server
or nil , errmsg
on failure.
The returned connection object may be this connection object itself.
Returns a table describing databases on the server.
databases.name: string
databases.empty: boolean
databases.sizeOnDisk: number
Shutsdown the server. Returns nothing.
Returns a database object, or nil.
Returns 1 in case of success, or nil with error message.
Returns a collection object for more operations.
Returns 1 in case of success, or nil with error message.
Returns number of rows been updated or nil for error.
1
, the database will insert the supplied object into the collection if no matching document is found, default to 0
.1
, the database will update all matching objects in the collection. Otherwise only updates first matching doc, default to 0
. Multi update only works with $ operators.0
. If 1
, the program will issue a cmd getlasterror
to server to query the result. If false
, return value n
would always be -1
Returns 0 for success, or nil with error message.
0
or false
. If 1
or `true
, the program will issue a cmd getlasterror
to server to query the result. If false
, return value n
would always be -1
Returns number of rows been deleted, or nil with error message.
0
0
. If 1
, the program will issue a cmd getlasterror
to server to query the result. If false
, return value n
would always be -1
Returns a single element array, or nil.
{n=0}
or {n=1}
Returns a cursor object for excuting query.
{n=0}
or {n=1}
1
, 0
for no limit, default to 100
.Returns the next item and advances the cursor.
A handy wrapper around cursor:next() that works in a generic for loop:
for index, item in cursor:pairs() do
Limits the number of results returned.
Returns an array with size size
sorted by given field.
1
for ascending sort, or -1
for descending sort. 10000
.under developing
Returns a gridfs file object.
Returns number of files been deleted, or nil with error message.
0
0
. If 1
, the program will issue a cmd getlasterror
to server to query the result. If false
, return value n
would always be -1
Writes first object matchs fields into file_handler. This API will malloc a buffer in file size in memory.
Returns 0 for success, or nil with error message.
_id, filename, chunkSize, contentType, aliases, metadata
or anything the user wants to store. Default meta.filename is the object id in string.0
. If 1
, the program will issue a cmd getlasterror
to server to query the result. If false
, return value n
would always be -1
Returns a new gridfs file object, or nil with error message.
_id, filename, chunkSize, contentType, aliases, metadata
or anything the user wants to store. Default meta.filename is the object id in string.Returns number of bytes read from mongodb, or nil with error message.
Returns number of bytes writen into mongodb, or nil with error message.
Hashs the file content and updates the md5 in file collection.
local mongo = require "resty.mongol"
conn = mongo:new()
conn:set_timeout(1000)
ok, err = conn:connect()
if not ok then
ngx.say("connect failed: "..err)
end
local db = conn:new_db_handle ( "test" )
col = db:get_col("test")
r = col:find_one({name="dog"})
ngx.say(r["name"])
> config = {_id: 'testset', members: [
{_id: 0, host: '10.6.2.51:27017'},
{_id: 1, host: '10.6.2.51:27018'},
{_id: 2, host: '10.6.2.51:27019'}]
}
> rs.initiate(config);
nohup bin/mongod --dbpath=/data/57cbd36d-5b70-4888-8537-bea71119363e/mongodb --oplogSize 10 --rest --replSet testset --port 27017 --keyFile key.file &
nohup bin/mongod --dbpath=/data/0a9419ae-4ec3-48c2-ad8d-df68a09aed13/mongodb --oplogSize 10 --rest --replSet testset --port 27018 --keyFile key.file &
nohup bin/mongod --dbpath=/data/8ee9efc0-a854-4c45-8893-7b4cb9ed0e5f/mongodb --oplogSize 10 --rest --replSet testset --port 27019 --keyFile key.file &
> use test
> db.addUser("admin","admin")