isage / lua-resty-moongoo

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

doc.cursor.id ~= cbson.uint(0) #21

Open misschuer opened 6 years ago

misschuer commented 6 years ago

function _M.aggregate(self, pipeline, opts) .............................. -- cursor return cursor.new(self, {}, {}, false, doc.cursor.id):add_batch(doc.cursor.firstBatch) end

OR

function _M.aggregate(self, pipeline, opts) .............................. -- cursor return cursor.new(self, {}, {}, false, cbson.uint(doc.cursor.id)):add_batch(doc.cursor.firstBatch) end

isage commented 6 years ago

What?

misschuer commented 6 years ago

sorry for

misschuer commented 6 years ago

when i use 'aggregate' to search data, then program runs err. so i explain the code, then i found 'doc.cursor.id' is not equal to cbson.uint(0) forever. i changed the 'doc.cursor.id' to 'cbson.uint(doc.cursor.id)' and it runs well. is this a bug or i use it in wrong way? it is in collection.lua line 372. forgive my poor english.

isage commented 6 years ago

Can you provide an example of how you use aggregate, and what error it returns?

misschuer commented 6 years ago

the pipeline: local wheres = { [ 1 ] = { ["$match"] = { guid = guid, } }, [ 2 ] = { ["$sort"] = { _id = -1, }, }, [ 3 ] = { ["$skip"] = skips, }, [ 4 ] = { ["$limit"] = limits, }, [ 5 ] = { ["$lookup"] = { from = user_table_name, localField = "guid", foreignField = "_id", as = "docs1", } }, [ 6 ] = { ["$project"] = { _id = 1, guid = 1, docs1 = { name = 1, avatar = 1, }, } }, }

This is copy from collection.lua

function _M.aggregate(self, pipeline, opts) local opts = opts or {} opts.pipeline = pipeline if not opts.explain then opts.cursor = {} end

local doc, err = self._db:cmd( { aggregate = self.name }, opts ) if not doc then return nil, err end

if opts.explain then return doc end

-- collection if opts.pipeline[#opts.pipeline]['$out'] then return self.new(opts.pipeline[#opts.pipeline]['$out'], self._db) end

--############### i debug it here ngx.log(ngx.ERR, "cursor_id = ", tostring(doc.cursor.id)) -- cursor return cursor.new(self, {}, {}, false, doc.cursor.id):add_batch(doc.cursor.firstBatch) end

the log file said 2018/07/12 20:52:15 [error] 27564#27564: 539 [lua] collection.lua:372: aggregate(): cursor_id = 0 2018/07/12 20:52:15 [error] 27564#27564: 539 [lua] utils.lua:425: print_err_format(): err : wrong cursor id

then i debug in cursor.lua function _M.next(self) ...... ngx.log(ngx.ERR, tostring(self._id)) if (not self._started) and (self._id == cbson.uint(0)) then ....... elseif #self._docs == 0 and self._id ~= cbson.uint(0) then ---############## Notice: because self._id is always 0 not eq to cbson.uint(0), it runs this logical and then throwed 'wrong cursor id' exception

if check_bit(flags, 0) then -- QueryFailure
  return nil, "wrong cursor id"
end

elseif #self._docs == 0 then--or self._id == cbson.uint(0) then return nil, "no more data" end ..... end