mongo 支持 hint (#1865)

* mongo 支持 hint

mongodb查询有时不会命中最优索引,所以支持下 hint 使用
This commit is contained in:
zhuilang
2024-02-02 15:58:57 +08:00
committed by GitHub
parent d0b11c7955
commit 6f24b08f02

View File

@@ -511,9 +511,10 @@ function mongo_collection:find(query, projection)
__data = nil,
__cursor = nil,
__document = {},
__sort = empty_bson,
__skip = 0,
__limit = 0,
__sort = empty_bson,
__hint = nil
} , cursor_meta)
end
@@ -548,6 +549,11 @@ function mongo_cursor:limit(amount)
return self
end
function mongo_cursor:hint(indexName)
self.__hint = indexName
return self
end
function mongo_cursor:count(with_limit_and_skip)
local ret
if with_limit_and_skip then
@@ -690,6 +696,13 @@ function mongo_collection:aggregate(pipeline, options)
} , aggregate_cursor_meta)
end
local function add_hint(self)
local h = self.__hint
if h then
return "hint", h
end
end
function mongo_cursor:hasNext()
if self.__ptr == nil then
if self.__document == nil then
@@ -701,7 +714,7 @@ function mongo_cursor:hasNext()
if self.__data == nil then
local name = self.__collection.name
response = database:runCommand("find", name, "filter", self.__query, "sort", self.__sort,
"skip", self.__skip, "limit", self.__limit, "projection", self.__projection)
"skip", self.__skip, "limit", self.__limit, "projection", self.__projection, add_hint(self))
else
if self.__cursor and self.__cursor > 0 then
local name = self.__collection.name