From bc4800f326f3ea4844810e412e805576f8a7110c Mon Sep 17 00:00:00 2001 From: ykxpb Date: Sat, 24 Sep 2022 08:41:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20mongo=20=E6=B8=B8=E6=A0=87?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E6=95=B0=E6=8D=AE=20hasNext=20=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=20true=EF=BC=8C=E4=BB=A5=E5=8F=8A=E6=97=A0=E6=95=88?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E6=B8=B8=E6=A0=87=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#1653)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: yy151474 --- lualib/skynet/db/mongo.lua | 16 ++++++++++++---- test/testmongodb.lua | 9 +++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 00ae86d5..d51b4d14 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -14,6 +14,7 @@ local table = table local bson_encode = bson.encode local bson_encode_order = bson.encode_order local bson_decode = bson.decode +local bson_int64 = bson.int64 local empty_bson = bson_encode {} local mongo = {} @@ -489,7 +490,10 @@ end function mongo_collection:findOne(query, projection) local cursor = self:find(query, projection) - return cursor:hasNext() and cursor:next() + if cursor:hasNext() then + return cursor:next() + end + return nil end function mongo_collection:find(query, projection) @@ -686,7 +690,7 @@ function mongo_cursor:hasNext() else if self.__cursor and self.__cursor > 0 then local name = self.__collection.name - response = database:runCommand("getMore", bson.int64(self.__cursor), "collection", name, "batchSize", self.__limit) + response = database:runCommand("getMore", bson_int64(self.__cursor), "collection", name, "batchSize", self.__limit) else -- no more self.__document = nil @@ -719,6 +723,10 @@ function mongo_cursor:hasNext() self.__limit = limit end + if cursor.id == 0 and #self.__document == 0 then -- nomore + return false + end + return true end @@ -739,9 +747,9 @@ function mongo_cursor:next() end function mongo_cursor:close() - if self.__cursor then + if self.__cursor and self.__cursor > 0 then local coll = self.__collection - coll.database:send_command("killCursors", coll.name, "cursors", {self.__cursor}) + coll.database:send_command("killCursors", coll.name, "cursors", {bson_int64(self.__cursor)}) self.__cursor = nil end end diff --git a/test/testmongodb.lua b/test/testmongodb.lua index bff60fab..89033e99 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -79,11 +79,20 @@ function test_find_and_remove() db.testcoll:dropIndex("*") db.testcoll:drop() + local cursor = db.testcoll:find() + assert(cursor:hasNext() == false) + db.testcoll:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"}) ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 1}) assert(ok and ret and ret.n == 1, err) + cursor = db.testcoll:find() + assert(cursor:hasNext() == true) + local v = cursor:next() + assert(v) + assert(v.test_key == 1) + ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 2}) assert(ok and ret and ret.n == 1, err)