bigplum / lua-resty-mongol Goto Github PK
View Code? Open in Web Editor NEWThis project forked from daurnimator/mongol
A ngx_lua driver for mongodb -- Deprecated for not updating with the mongodb version
This project forked from daurnimator/mongol
A ngx_lua driver for mongodb -- Deprecated for not updating with the mongodb version
when sort key is null will be crash
fix it
local sort_f = function(a, b)
if not a and not b then return false end
if not a then return true end
if not b then return false end
if not a[key] and not b[key] then return false end
if not a[key] then return true end
if not b[key] then return false end
if asc == 1 then
return a[key] < b[key]
else
return a[key] > b[key]
end
end
How to insert date field? like a new Date('Dec 03, 1924'),
使用 cursor = col:find(query, returnfields, num_each_query) 进行查询数据库的时候返回空
local cursor = table::find()
这样使用不行么?
When trying to run the module I constantly get the following error
*[error] 21872#0: 2 lua entry thread aborted: runtime error: /etc/nginx/lua/mongol.lua:11: attempt to call method 'new' (a nil value)
The code I have is the following
-- Includes
local p = "/usr/local/Cellar/lua-resty-mongol/devel/sharelua-resty-mongol/lualib/"
local m_package_path = package.path
package.path = string.format("%s?.lua;%s?/init.lua;%s", p, p, m_package_path)
-- Initialize
local mongo = require "resty.mongol"
if not mongo then
ngx.say("Could not get mongol instance")
end
conn = mongo:new()
If I change the include path I get module not found as expected, so it's definitely including the module, just not working correctly.
Am I missing something obvious?
Thanks
/Marc
When using Monngo db 3.0 with auth enabled, the default authentication enabled, Its SCRAM-SHA-1.
Now when i try to authenticate using db:auth(username, password) method, it fails with teh err
Failed to authenticate admin@gridfs with mechanism MONGODB-CR: AuthenticationFailed: MONGODB-CR credentials missing in the user document
如果不带连接池 是每次都请求后都要close()?
Maybe I misunderstood something, but how? Aren't cursor:next and pairs methods, which generates query to db on each record, instead of getting all needed by one request? col:getmore seems to be right method, which will return any number of recs, but my cursor id somehow always is false
As we know, when we iterate table with pairs in lua, it doesn't guarantee the order as we defines in table.
http://www.lua.org/manual/5.2/manual.html#pdf-next
if onlyarray then
if t_k == "number" and k >= 0 then
if k >= high_n then
high_n = k
seen_n [ k ] = v
end
else
onlyarray = false
end
end
the code above deals array in to_bson in bson.lua, array value will store in seen_n only if k >= high_n, however this condition won't always be true. It means we may lost some value and results in 'null' stores in mongodb.
I think seen_n [ k ] = v should be moved out of the if condition and we can hold all the data.
Hey bigplum,
tl;dr :: I'm refactoring this entire library to look more like lua and less like C. Also adding a rockspec for this and the resty bindings library. Should I make a PR or just create a couple new repos?(EG:: are you still interested in maintaining this)
So I am writing an event based web framework that's quite different from anything in any language for ngx_lua. It's called lusty https://github.com/Olivine-Labs/lusty and consists of many tiny modules. I'm currently writing a RESTful storage adapter for mongo(IE::fire put event, old document gets deleted and new gets inserted into configured collection, multiple adapters allowed for each model type so you can store data in multiple database types easily using predicates) and the interface for this library is wonky and the code is messy and I'm OCD about those things.
Basically I'm looking to rewrite this codebase and break every api it has. I'm also talking to daurnimator about perhaps merging this fork upstream and doing a pull request to his repo. My goals here are to unify the disparate repositories for this project and hopefully get one really nice version out there for everyone using openresty/ngx_lua to enjoy.
Thanks,
DorianGray
使用openresty1.4.7 lua_resty_mongol链接MongoDB Nginx配置如下:
location /mongo {
content_by_lua '
local mongo = require "resty.mongol"
conn = mongo:new()
conn:set_timeout(1000)
local ok,err = conn:connect("192.168.1.100","27017")
if not ok then
ngx.say("链接失败"..err)
return
end
local db,err = conn:new_db_handle("igewen")
if not db then
ngx.say(err)
return
end
db:auth("igewen","123456")
local col = db:get_col("accounts")
local res = col:find_one({salt="0f2f93"})
ngx.say(res["email"])
';
}
访问出现500错误。
从Nginx debug日志看不出什么,不过Mongo日志出现下面的错误:
Mon Dec 17 21:31:26 [initandlisten] connection accepted from 192.168.1.102:58293 #3 (1 connection now open)
Mon Dec 17 21:31:26 [conn3] run command igewen.$cmd { getnonce: true }
Mon Dec 17 21:31:26 [conn3] command igewen.$cmd command: { getnonce: true } ntoreturn:1 keyUpdates:0 reslen:65 0ms
Mon Dec 17 21:31:26 [conn3] run command igewen.$cmd { authenticate: true, user: "igewen", nonce: "1bb17b627b06af9d", authenticate: true, key: "7a5c1317d90485e9245a89c6138ff655" }
Mon Dec 17 21:31:26 [conn3] authenticate db: igewen { authenticate: true, user: "igewen", nonce: "1bb17b627b06af9d", authenticate: true, key: "7a5c1317d90485e9245a89c6138ff655" }
Mon Dec 17 21:31:26 [conn3] command igewen.$cmd command: { authenticate: true, user: "igewen", nonce: "1bb17b627b06af9d", authenticate: true, key: "7a5c1317d90485e9245a89c6138ff655" } ntoreturn:1 keyUpdates:0 locks(micros) r:170 reslen:84 0ms
Mon Dec 17 21:31:26 [conn3] Socket recv() errno:104 Connection reset by peer 192.168.1.102:58293
Mon Dec 17 21:31:26 [conn3] SocketException: remote: 192.168.1.102:58293 error: 9001 socket exception [1] server [192.168.1.102:58293]
Mon Dec 17 21:31:26 [conn3] end connection 192.168.1.102:58293 (0 connections now open)
我的MongoDB版本是2.2.2。是否是与DB版本不兼容?
谢谢,支持!
Hi, is there a way to read from secondery of my replicaset? or is there a way to send "slaveOk=1" as command?
I have tried to do this
local n, err = sessions:update({ username = username }, { $set = { sessionid = base32 } }, 1, 0, 1)
But it returned this unexpected symbol near '$'
So how do I do this correctly?
I've try to sort document by some filed and then query using skip and limit, since
colmethods:query(query, returnfields, numberToSkip, numberToReturn, options)
have no sorting parameter or using
colmethods:find(query, returnfields, num_each_query)
which return cursor but cursor method have no skip function
is it possible to do something like this using lua-resty-mongol
If I insert an array like this
col:insert({ {photos={"1","2"} } })
I'll get an null element like this
{ "_id" : ObjectId("536650e213cb857d4ebfd65c"), "photos" : [ null, "1", "2" ] }
Is this a bug?
How can I use aggregate?
such as:
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
function colmethods:find_one(query, returnfields)
local id, results, t = self:query(query, returnfields, 0, 1)
if id == "\0\0\0\0\0\0\0\0" and results[1] then
return results[1]
end
return nil
end
This API returns a document not a single element array when document found.
我想实现分页查询的功能
目前使用
cursor = col:find({JOB_NAME="JOB_NAME"},{},10)
可以返回我想要的数据量,但是不知道使用哪个api来区分页码,请提供一个示例程序,多谢。
Hey guys!
There is a string: https://github.com/bigplum/lua-resty-mongol/blob/master/lib/resty/mongol/object_id.lua#L56
ngx.var api is disabled inside ssl_certificate_by_lua* derectives. Is it really necessary to use ngx.var? Mb we can use something instead?
use batch operation to decrease number of requests to mongodb
How to implement 'findAndModify' ?
in mycode
sel = {};
sel["$or"]={{fuid='51a8e9d639f85f4caa5a789f'},{tuid='51a8e9d639f85f4caa5a789f'}};
and in mongodb log
assertion 13263 $or array must contain objects ns:icredit.transactions query:{ $or: [ null, { fuid: "51a8e9d639f85f4caa5a789f" }, { tuid: "51a8e9d639f85f4caa5a789f" } ] }
I don't know why mongol send null for first element
trying to define empty table:
local tab = {}
and then add it to data:
local data = {}
table.insert(data,{tab = tab})
(then insert it as new document)
will result as empty object (instead of empty array):
{
tab = {
}
}
We got a report that lua-resty-mongol stores "chunkSize" as a BSON float instead of an int. This causes PyMongo to throw an exception trying to read GridFS files stored by lua-resty-mongol, and it will likely cause other MongoDB drivers to throw exceptions as well:
https://jira.mongodb.org/browse/PYTHON-749
The symptom will be that when you query the "files" collection using the Mongo shell, you can find documents where chunkSize is a float type:
db.fs.files.find({chunkSize: {$type: 1}})
... but not int32 type:
db.fs.files.find({chunkSize: {$type: 16}})
If the driver is storing files in GridFS correctly, then chunkSize will be int type: $type: 16
.
Is this the right place to file this, or shall I file it on the upstream "mongol" project?
Uncatchable error occured: '/usr/local/openresty/lualib/resty/mongol/colmt.lua:78: assertion failed!'
It seems to happen randomly so it's hard to reproduce the error.
Any ideas?
Hi, Is there way I can use read preference?
I like to set reading from slave preferred. I am not sure whether current driver has support or any workaround existing..
I try to test query command
id, results, t = col:query(sel,{_id=1},0,1);
id, results, t = col:query(sel,{_id=1},5,5);
i got 10. should be 5
What's wrong ?
How to use mongol for query like this
db.inventory.find( { $or: [ { qty: { $gt: 100 } },
{ price: { $lt: 9.95 } } ]
} )
how to call javascript on server ? like this
db.users.insert(
{
_id: getNextSequence("userid"),
name: "Sarah C."
}
)
cursor:limit(6)
local sort_res = cursor:sort({startTime=-1}}, 1000)
当最大条目小余limit值,最开始的几条数据不会返回~~这个是怎么回事?
比如最大条目是7,limit为10,则全部返回,limit为5,则最大的两条不会返回
when use the lua-resty-mongol in ngx.timer context, a error occured as follows:resty/mongol/object_id.lua:56: API disabled in the current context, context: ngx.timer,
the reason is the ngx.var.pid can not use in ngx.timer context, and at last I use ngx.worker.pid() instead.
As per this post querying mongo on partial sub array via dot notation doesnt work with this module (at least not that ive been able to get working). If it is available its not documented anywhere?
http://stackoverflow.com/questions/11414300/query-for-partial-object-in-array-mongodb
比如文档中说的conn:settimeout(msec), 实际上代码内写的是conn:set_timeout(msec).
文档内说的conn:cmd这个mothod没有,引用会报nil错误.
Hi, I have a problem with working with object ID.
I like to use object ID to be a unique id for a document. And, when I need to serialize that to string so tht I can send as JSON doc, it works with objectID's tostring() function.
Now, when I receive the string, I tried to convert that to object ID and it asserts. It's because object ID was 12 byte, and when the object ID converted to string (hex string %02x), it becomes 24 bytes. Now, when I receive the data, your library check whether the side is 12 bytes so it won't work.
Even though I removed the assert to see whether it will convert back, it does not.
So, there seem to be no way that I can convert to object ID again from the string so that I can run a query based on that.. I also use PyMongo in my python cron job, but, it works fine in that library.
any thought? or any good idea to work around? I tried many different method, but, i just cannot get back to the obj id.
你好,我在安装的openresty,添加mongol的库文件到resty,在连接mongol成功后,鉴权总是失败,检查账号密码没有问题,鉴权这里是不是代码有bug,希望可以改进。
I got
[error] 67343#0: *24 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/mongol/ll.lua:36: assertion failed!
Hi,
I want to get the below object.
{
"_id" : "hoge",
"sInfo" : [
{
"sToken" : "testToken",
"sTokenExpirationDate" : ISODate("2015-12-15T02:01:06.802Z")
}
]
}
So, i built a query to get sInfo.sToken.
The query is :
local query = {
sInfo = {
{
sToken = "testToken"
}
}
}
local r = col:find_one(query)
ngx.log(ngx.ERR, r["_id"])
But, the variable of 'r' is nil.
How can i get 'sInfo.sToken' ?
local mongo = require "resty.mongol"
local host_info = {
host="192.168.1.2",
port=27017,
username="ttt",
password="ttt"
}
conn = mongo:new()
conn:set_timeout(2000)
ok, err = conn:connect(host_info["host"], host_info["port"])
ngx.say(ok,err)
-- 1 nil
if not ok then
ngx.log("connect failed: "..err)
end
local db = conn:new_db_handle("ttt")
ok, err = db:auth(host_info["username"], host_info["password"])
ngx.say(ok, err)
-- nil lauth failed
col = db:get_col("tt")
r = col:find_one({a=1})
if r == nil then
return false
else
ngx.say(r["_id"])
return true
end
in mongodb database, if a record has field value -1 , and its datatype=int32 ,
then use lua-resty-mongol will get value 4294967295 。
I guess it is a overflow problem ,but how to solve it ? thank you very much!
local versions = {}
local t = {}
table.insert(t,{a = "aa"})
table.insert(t,{b = "bb"})
versions={{name="dog",container="mycontainer",tab= t}}
mongos> db.files.find({container:'mycontainer'}).pretty()
{
"_id" : ObjectId("4fbb710018b42f60c829ae87"),
"container" : "mycontainer",
"tab" : [
undefined,
{
"a" : "aa"
},
{
"b" : "bb"
}
],
"name" : "dog"
}
Also, pushing new info to "tab" results in resetting "tab" into new array of:
[
undefined,
{
"c" : "cc"
}
]
Hi there,
There's some inconsistency in the naming on this library.
dbmt.lua
, there's listcollections
, dropDatabase
& add_user
. I recommend sticking to a single policy there. Since mongo's interface is camelCased, I recommend camelCase.Here is how to reproduce:
in access :
local ok, object = DB.query('collection',search)
ngx.ctx.env.object = object
in content:
local object = ngx.ctx.env.object
local search = {}
search.something = object._id
search.container = ngx.ctx.env.containerName
DB.query('collection',search)
-> I think there the object is not properly kept in ngx.ctx, and then cannot be passed to another
query
Dumping the object used in content:
search contains:
container, and object as object._id
Logger.debug('--->')
for k,v in pairs(search) do
if type(v) ~= 'table' then
Logger.debug( k..' -> '..v)
else
for i,j in pairs(v) do
Logger.warn(i..'->'..type(j))
end
end
end
Logger.debug('<---')
--->
get_inc->function, ....
id->string, ...
get_hostname->function, ...
get_pid->function, ...
tostring->function, client: ...
get_ts->function, client...
container -> 'xxxx' # (this is real data, existing)
<---
nb: the same technic, reloading the object in 'content', works perfectly.
is there a way to reuse the connection socket? I got this error when I ab it with big amount of connection.
[crit] 1207#0: *10119 connect() to 127.0.0.1:27017 failed (49: Can't assign requested address), client: 127.0.0.1,
2018/12/22 14:19:20 [error] 12243#0: init_worker_by_lua_file error: /usr/local/openresty/lualib/resty/mongol/object_id.lua:56: API disabled in the current context
stack traceback:
[C]: in function '__index'
/usr/local/openresty/lualib/resty/mongol/object_id.lua:56: in main chunk
[C]: in function 'require'
/usr/local/openresty/lualib/resty/mongol/bson.lua:25: in main chunk
[C]: in function 'require'
/usr/local/openresty/lualib/resty/mongol/colmt.lua:20: in main chunk
[C]: in function 'require'
/usr/local/openresty/lualib/resty/mongol/dbmt.lua:9: in main chunk
[C]: in function 'require'
/usr/local/openresty/lualib/resty/mongol/init.lua:14: in main chunk
[C]: in function 'require'
/home/proj/gamebin//logic/banksafer.lua:3: in main chunk
[C]: in function 'require'
/home/proj/gamebin//handler/centerhandler.lua:9: in main chunk
[C]: in function 'require'
/home/proj/gamebin//libs/center.lua:11: in main chunk
[C]: in function 'require'
/home/proj/gamebin/libs/worker_init.lua:2: in main chunk
Running into this error even with the simplest code.
mongol = require "resty.mongol"
conn = mongol:new()
Get the "attempt to call method 'new' (a nil value)" error on line 2.
Running off of github clone. Basic Lua hello world is working in Openresty. Any clues?
Hi,
In MongoDB this command created a collection if it does not exist: db.collection.insert()
I see no way of creating a collection like this in mongol.
Am i missing something here?
Thanks for your help.
Samar
It seems there is a issue inserting Date
type.
For instance, I have a code to create new user with created_at
field.
local mongo = require "resty.mongol"
local bson = require "resty.mongol.bson"
local now = bson.get_utc_date(ngx.time() * 1000)
local data = {
user = 1,
created_at = now
}
local db, err = mongo:new(XXXXX)
local user = db:get_col("user")
local res, err = user:insert({data}, nil, true)
Data stored in created_at
is actually {"v"=>1469128796000}
and the value 1469128796000
is stored as int64
type, not mongo's Date
type.
Does anybody has same issue or a suggestion to fix it?
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.