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 end
function mongo_collection:findOne(query, projection) function mongo_collection:findOne(query, projection)
local cursor = self:find(query, projection) local r = self.database:runCommand("find", self.name, "filter", query and bson_encode(query) or empty_bson,
if cursor:hasNext() then "limit", 1, "projection", projection and bson_encode(projection) or empty_bson)
return cursor:next() if r.ok ~= 1 then
error(r.errmsg or "Reply from mongod error")
end end
return nil return r.cursor.firstBatch[1]
end end
function mongo_collection:find(query, projection) function mongo_collection:find(query, projection)