isage / lua-resty-moongoo

MongoDB library for OpenResty
Do What The F*ck You Want To Public License
118 stars 33 forks source link

can't find data when query as NumberLong #12

Closed hellojjb closed 6 years ago

hellojjb commented 7 years ago

can't find data when query as NumberLong. Example:

data: { "_id": "ObjectId("593617ea8f42a74bc053ac01")", "pk_eid": "NumberLong(1474624577207277091)", "opttime": "NumberLong(1496717290928)", "mobile":"13567679090" }

code: local moongoo = require("resty.moongoo") local cbson = require("cbson")

local mg, err = moongoo.new("XXXXXXX") if not mg then error(err) end

local col = mg:db("test"):collection("test") local eid = cbson.int("1474624577207277091") local doc, err = col:find_one({ pk_eid = eid}) if err then ngx.say('err:'..err); return; end if not doc then ngx.say('nil') return; end ngx.say(doc.mobile);

result: err:No more data

How should I do? Please help me! I look forward to your reply!

isage commented 7 years ago

Well, it does work, actually. find_one always returns 'No more data', which may be a little misleading. So you should first check if doc is nil, and only then check for error:

local moongoo = require("resty.moongoo")
local cbson = require("cbson")

local mg, err = moongoo.new("XXXXXXX")
if not mg then
error(err)
end

local col = mg:db("test"):collection("test")
local eid = cbson.int("1474624577207277091")
local doc, err = col:find_one({ pk_eid = eid})

if not doc and err then
ngx.say('err:'..err);
return;
end

ngx.say(doc.mobile);
hellojjb commented 7 years ago

Thank U for you reply! I very sure the doc is not nil.

doc:
{
"_id": "ObjectId("593617ea8f42a74bc053ac01")",
"pk_eid": "NumberLong(1474624577207277091)",
"opttime": "NumberLong(1496717290928)",
"mobile":"13567679090"
}
col:find_one({ mobile = "13567679090"})

can find some data by this way.

local doc, err = col:find_one({ pk_eid = cbson.int("1474624577207277091")})

return no more data by this way.

Output in the MongoDB shell shows:

> db.test.find({ "pk_eid" : NumberLong("1474624577207277091")}).limit(10)
{
"_id": "ObjectId("593617ea8f42a74bc053ac01")",
"pk_eid": "NumberLong(1474624577207277091)",
"opttime": "NumberLong(1496717290928)",
"mobile":"13567679090"
}

this can find some data

Please help me! I look forward to your reply!

isage commented 7 years ago

look at the code i provided. find_one always returns err, even on success. just check what it returns in doc

On Sep 1, 2017 11:51 AM, "hellojjb" notifications@github.com wrote:

Thank U for you reply! I very sure the doc is not nil.

doc: { "_id": "ObjectId("593617ea8f42a74bc053ac01")", "pk_eid": "NumberLong(1474624577207277091)", "opttime": "NumberLong(1496717290928)", "mobile":"13567679090" }

col:find_one({ mobile = "13567679090"})

can find some data by this way.

local doc, err = col:find_one({ pk_eid = cbson.int("1474624577207277091")})

return no more data by this way.

Output in the MongoDB shell shows:

db.test.find({ "pk_eid" : NumberLong("1474624577207277091")}).limit(10) { "_id": "ObjectId("593617ea8f42a74bc053ac01")", "pk_eid": "NumberLong(1474624577207277091)", "opttime": "NumberLong(1496717290928)", "mobile":"13567679090" }

this can find some data

Please help me! I look forward to your reply!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/isage/lua-resty-moongoo/issues/12#issuecomment-326527182, or mute the thread https://github.com/notifications/unsubscribe-auth/AAQtIckrUyy7-8A3exK7i4nxebwreBBSks5sd8WjgaJpZM4PIwlC .

hellojjb commented 7 years ago

Thank U for you reply! I used your code, returns in doc is nil; like this:

local moongoo = require("resty.moongoo")
local cbson = require("cbson")

local mg, err = moongoo.new("XXXXXXX")
if not mg then
error(err)
end

local col = mg:db("test"):collection("test")
local eid = cbson.int("1474624577207277091")
local doc, err = col:find_one({ pk_eid = eid})

ngx.say(doc);   --doc return nil;

if not doc and err then
ngx.say('err:'..err);   --err return no more data
return;
end

I've tried.

hellojjb commented 7 years ago

Is it because the "eid" is larger than the 2^53?

isage commented 7 years ago

No, lua-cbson uses int64_t, which is the same as NumberLong in mongo. Moreover, i've tested with your data and it works.

Can you provide mongodb version, OS version and architecture (32/64 bit) ?

hellojjb commented 7 years ago

mongodb version is 3.2 OS version is CentOS 6.5 64 bit openresty version is 1.9.7.4

hellojjb commented 7 years ago

I did it. like this

local eid = cbson.int("1474624577207277091".."LL")

Thank you so mach!