findOne优化 (#1772)

1、直接调用runCommand方法
2、增加"limit": 1参数,限制只返回一条数据
This commit is contained in:
Whislly
2023-07-10 12:29:16 +08:00
committed by GitHub
parent 074426e273
commit c6a8976bc3

View File

@@ -494,11 +494,12 @@ function mongo_collection:raw_safe_delete(delete)
end
function mongo_collection:findOne(query, projection)
local cursor = self:find(query, projection)
if cursor:hasNext() then
return cursor:next()
local r = self.database:runCommand("find", self.name, "filter", query and bson_encode(query) or empty_bson,
"limit", 1, "projection", projection and bson_encode(projection) or empty_bson)
if r.ok ~= 1 then
error(r.errmsg or "Reply from mongod error")
end
return nil
return r.cursor.firstBatch[1]
end
function mongo_collection:find(query, projection)